0

I am trying to run a WSGI app as described here https://aiohttp-wsgi.readthedocs.io/en/stable/wsgi.html

The following code works:

app = web.Application()
app.router.add_route("*", "/{path_info:.*}", wsgi_handler)

But, I want to add path like /hello, so I tried following few things, none of which worked. For example:

Approach 1:

app.router.add_route("*", "/hello", wsgi_handler)

gives me 500 Internal Server Error in the browser and on console it says:

Error handling request
Traceback (most recent call last):
  File "/media/raghav/workspace/anaconda3/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/media/raghav/workspace/anaconda3/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/media/raghav/workspace/anaconda3/lib/python3.7/site-packages/aiohttp/web_urldispatcher.py", line 158, in handler_wrapper
    return await result
  File "/media/raghav/workspace/anaconda3/lib/python3.7/site-packages/aiohttp_wsgi/wsgi.py", line 261, in handle_request
    environ = self._get_environ(request, body, content_length)
  File "/media/raghav/workspace/anaconda3/lib/python3.7/site-packages/aiohttp_wsgi/wsgi.py", line 182, in _get_environ
    path_info = request.match_info["path_info"]
KeyError: 'path_info'

Approach 2:

app.router.add_route("*", "/{path_info: hello}", wsgi_handler)

gives 404: Not Found error in the browser and says nothing on terminal.

How should I add a route?

jubnzv
  • 1,526
  • 2
  • 8
  • 20
user2698178
  • 366
  • 2
  • 11

1 Answers1

0

Try this one:

app.router.add_route("*", "/{path_info:hello.*}", wsgi_handler)

Complete example:

from aiohttp import web
from aiohttp_wsgi import WSGIHandler


def noop_application(environ, start_response):
    start_response("200 OK", [
        ("Content-Type", "text/plain"),
    ])
    return []


if __name__ == '__main__':
    wsgi_handler = WSGIHandler(noop_application)
    app = web.Application()
    app.router.add_route("*", "/{path_info:hello.*}", wsgi_handler)
    web.run_app(app)
jubnzv
  • 1,526
  • 2
  • 8
  • 20