【问题标题】:Python 3.4.3 & Bottle with CGI - environ['REQUEST_METHOD']带有 CGI 的 Python 3.4.3 和 Bottle - environ['REQUEST_METHOD']
【发布时间】:2016-06-07 01:11:33
【问题描述】:

我正在尝试使用 Python 3.4.3 和 Bottle 0.12.8 使用 cgi 运行一个简单的 Web 服务。我正在从我的 Linux 系统运行以下脚本。我能够在没有 CGI 的情况下运行相同的服务。

======================================
import bottle
from bottle import route, run, request, response

host = 'XXXX'
port = '8080'
debug = 'False'

@route('/hello/',  method=['OPTIONS','GET'])
def hello():
    return("Success")

bottle.run(host=host, port=port,debug=debug,server='cgi')
#bottle.run(host=host, port=port,debug=debug)
======================================

当我使用 CGI 运行服务时出现以下错误-

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/bottle.py", line 858, in _handle
    route, args = self.router.match(environ)
  File "/usr/local/lib/python3.4/site-packages/bottle.py", line 413, in match
    verb = environ['REQUEST_METHOD'].upper()
KeyError: 'REQUEST_METHOD'
<h1>Critical error while processing request: </h1><h2>Error:</h2>
<pre>
KeyError(&#039;REQUEST_METHOD&#039;,)
</pre>
<h2>Traceback:</h2>
<pre>
Traceback (most recent call last):
  File &quot;/usr/local/lib/python3.4/site-packages/bottle.py&quot;, line 957, in wsgi
    or environ[&#039;REQUEST_METHOD&#039;] == &#039;HEAD&#039;:
KeyError: &#039;REQUEST_METHOD&#039;

</pre>
Status: 500 INTERNAL SERVER ERROR
Content-Type: text/html; charset=UTF-8
Content-Length: 374

<h1>Critical error while processing request: </h1><h2>Error:</h2>
<pre>
KeyError(&#039;REQUEST_METHOD&#039;,)
</pre>
<h2>Traceback:</h2>
<pre>
Traceback (most recent call last):
  File &quot;/usr/local/lib/python3.4/site-packages/bottle.py&quot;, line 957, in wsgi
    or environ[&#039;REQUEST_METHOD&#039;] == &#039;HEAD&#039;:
KeyError: &#039;REQUEST_METHOD&#039;

</pre>

Any pointers would help. Thanks

【问题讨论】:

    标签: python bottle


    【解决方案1】:

    您收到此错误是因为 basic CGI HTTP server in Python 仅支持 GET、HEAD 和 POST 命令。 CGI 服务器在尝试验证 OPTIONS 命令时抛出 KeyError。如果你想在你的服务器代码中使用 OPTIONS 方法,你需要自己实现它或者切换到不同的服务器。

    【讨论】:

    • 谢谢卡希特。我现在在我的实现中使用了 CherryPy。
    猜你喜欢
    • 1970-01-01
    • 2015-01-29
    • 2015-04-10
    • 2020-05-30
    • 2023-03-29
    • 2016-05-02
    • 2017-03-31
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多