1. Home
  2. Computing & Technology
  3. Python

Building a Simple Web Server in Python

By Al Lukaszewski, About.com

8 of 10

Handling a Server Request

Having setup the server, we now need to tell Python what to do when a request is made on the given port. For this we reference the request by its value and use it as the argument of a persistent while loop.

When a request is made, the server should accept the request and create a file object to interact with it.

while 1:
csock, caddr = c.accept()
cfile = csock.makefile('rw', 0)

In this case, the server uses the same port for reading and writing. Therefore, the makefile method is given an argument 'rw'. The null length of the buffer size simply leaves that part of the file to be determined dynamically.

8 of 10

Explore Python

More from About.com

  1. Home
  2. Computing & Technology
  3. Python
  4. Networking
  5. Python Web Server: Handling a Server Request

©2008 About.com, a part of The New York Times Company.

All rights reserved.