1. Home
  2. Computing & Technology
  3. Python

Building a Simple Web Server in Python

From Al Lukaszewski, for 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.

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.