简体   繁体   中英

How to Begin Python Web Programming with WFastCGI + IIS + Python 3.4?

As a complete python newbie who just completed the free lessons on codeacademy, I followed the instructions on this Python wfastcgi 2.2 page and sucessfully installed the python handler on IIS.

Then I created a python file (module) my_app.py as per the web.config with the following code (which I adapted from somewhere):

def wsgi_app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

Upon navigating to the localhost site, IIS returns the following error:

Error occurred while reading WSGI handler:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\wfastcgi.py", line 779, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "C:\Python34\lib\site-packages\wfastcgi.py", line 621, in read_wsgi_handler
    handler = get_wsgi_handler(os.getenv('WSGI_HANDLER'))
  File "C:\Python34\lib\site-packages\wfastcgi.py", line 594, in get_wsgi_handler
    handler = handler()
TypeError: wsgi_app() missing 2 required positional arguments: 'environ' and 'start_response'


StdOut: 

StdErr:

Question :

  1. How and what do I pass for the 2 function arguments?
  2. Starting the module with a function does not seem like a good idea. Can I use a class instead? If so, how should I change my web.config on this line:
<add key="WSGI_HANDLER" value="my_app.wsgi_app()" />

My first recommendation (if you haven't done so already) is to configure Failed Request Tracing in IIS. Then whenever your WSGI handler (ie my_app.wsgi_app) crashes during development IIS will generate a nice .xml file, that you can view in a browser, detailing exactly what happened, including the Python traceback, even if your IIS is ultimately configured to generate "500 - Internal server error" in this instance.

Next, as Daniel Roseman has already suggested, change

<add key="WSGI_HANDLER" value="my_app.wsgi_app()" />

to

<add key="WSGI_HANDLER" value="my_app.wsgi_app" />

in your web.config file and wfastcgi.py will be able to find and invoke your wsgi_app.

Finally, your wsgi_app as shown will (I believe) fail with a Python traceback as follows:

File "<the path to ...\python\pythonXX\lib\site-packages\wfastcgi.py on your system>", line 372, in send_response
raise TypeError("content must be encoded before sending: %r" % content)
TypeError: content must be encoded before sending: 'Hello world!\n'

The solution to this encoding problem is described in David Beazley's "Python: Essential Reference" in the section on wsgiref (page 541 in my copy). I would suggest you try the following:

def wsgi_app(environ, start_response):
    status = "200 OK"
    headers = [("Content-Type", "text/plain; charset=utf-8")]
    start_response(status, headers)

    response = ["Hello world!\n"]

    return (line.encode("utf-8") for line in response)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM