Python | WSGI returns different content

WSGI different paths return different content


from wsgiref.simple_server import make_server


def demo_app(environ, start_response):
path = environ['PATH_INFO']

# Status code: RESTFUL ==> Front-end and back-end separation
# 2XX: The request response was successful
# 3XX: redirect
# 4XX: Client error. 404 The client accessed a non-existent address 405: The request method is not allowed
# 5XX: Server error.
status_code = '200 OK' # The default status code is 200
if path == '/':
response = 'Welcome to my homepage'
elif path == '/test':
response = 'Welcome Ali to the test page'
elif path == '/demo':
response = 'Welcome to the demo page'
else:
status_code = '404 Not Found' # If the page is not configured, return 404
response = 'The page is lost'

start_response(status_code, [('Content-Type', 'text/html;charset=utf8')])
return [response.encode('utf8')]


if __name__ == '__main__':
httpd = make_server('', 8080, demo_app)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00