Please go through the HTML Example before doing this example.

Create a sample python file

  1. Using nano, we can create tutorial.py:

    [username@l-lnx101 examples]$ nano tutorial.wsgi

  2. Copy and paste the following code into your nano screen (remember that white space is important in python, so it has to match exactly):

    import socket
    def application(environ, start_response):
      status = '200 OK'

      # Get IP information
      client_ip = environ['REMOTE_ADDR']
      hostname = socket.gethostbyaddr(client_ip)[0]

      # Generate html
      output = '<html><head><title>Sample Python Script</title></head>\n<body>\n'
      output +='REMOTE_ADDR is '+client_ip + ' <br> \n'
      output +="Lookup of "+client_ip+" is <b>"+hostname+"</b><br> \n"
      output += "</body></html>\n"

      # Return web page
      response_headers = [('Content-type', 'text/html'),
                                              ('Content-Length', str(len(output)))]
      start_response(status, response_headers)
      output2 = output.encode('utf-8')
      return [output2]

  3. Save the file and go to http://webdev.cs.uiowa.edu/webdev/COURSEID/USERNAME/tutorial.wsgi where COURSEID and USERNAME are replaced with your course number and hawkid.
  4. The page when viewed on l-lnx100 in the lab looks like:

    REMOTE_ADDR is 128.255.44.124
    Lookup of 128.255.44.124 is l-lnx100.divms.uiowa.edu