Python 3.4.3&带CGI的瓶子 - 环境[' REQUEST_METHOD']

时间:2016-02-24 16:17:24

标签: python bottle

我正在尝试使用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

1 个答案:

答案 0 :(得分:0)

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