WSGI Returns Different Content in Different Paths | Teach You to Get Started With Python One -hundred and Fourteen

WSGI Returns Different Content in Different Paths | Teach You to Get Started With Python One -hundred and Fourteen

This section describes the different content and HTTP status codes returned by different paths of WSGI.

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" , on [ 0 ], "port" , on [ 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