Python 3.4.3 & Bottle with CGI - environ['REQUEST_METHOD']
Question: I am trying to use Python 3.4.3 and Bottle 0.12.8 to run a simple web service
using cgi. I am running the below script from my Linux system. I was able to
run the the same service without 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)
    ======================================
    

I get the below error, when I run the service with 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
    


Answer: You're getting this error because the [basic CGI HTTP server in
Python](https://hg.python.org/cpython/file/3.4/Lib/http/server.py) only
supports GET, HEAD and POST commands. CGI server is throwing the KeyError when
it attempts to validate the OPTIONS command. If you want to use the OPTIONS
method in your server code, you'll need implement it yourself or switch to a
different server.

