HTTP headers problem
by Jeremy Aiyadurai other posts by this author
May 3 2002 2:18AM messages near this date
view in the new Beta List Site
Re: Low level python
|
Re: HTTP headers problem
hi all
here is a barebone webserver. I am having problems with the http headers. I
can get a page to load in
netscape, but not Internet explorer.
the following is the code. please pay close attention to the
getParse(),createHeader() and contentType() subroutines.
Your help is much appreciated.
Jeremy A.
############################################################################
#######
#!python
from socket import *
import re
from string import *
class webserver:
def __init__(self,port,wwwroot):
self.PORT = port
self.WWWROOT = wwwroot
def start(self):
print "Starting server..."
ss = socket(AF_INET,SOCK_STREAM)
ss.bind(("127.0.0.1",self.PORT))
print "Server Bound"
ss.setblocking(1)
ss.listen(1)
print "\nListening..."
self.handle(ss)
def handle(self, ss):
while 1:
client,addr = ss.accept()
self.Analyze(client)
def Analyze(self,client):
response = self.stringread(client)
method,url,proto = split(response," ")
proto = self.chomp(proto)
print method
result = None
response = self.stringread(client)
result = re.match('\S',response)
print result
while result == None:
response = self.stringread(client)
result = re.match('\S',response)
print result
if method == "GET":
self.getParse(url,proto,client)
def chomp(self,s):
if s[-2:] == '\r\n':
return s[:-2]
if s[-1:] == '\r' or s[-1:] == '\n':
return s[:-1]
return s
def getParse(self,url,proto,client):
if url == "/":
print "in"
url = "index.html"
htm = open(""+url+"","r")
buf = htm.read()
self.createHeader(url,proto,client)
client.send(buf)
client.shutdown(2)
def createHeader(self,url,proto,client):
print ""+proto+" 200 OK\n"
client.send(proto+" 200 OK\r\n")
self.contentType(url,client)
def contentType(self,url,client):
client.send("Content-Type: text/html\r\n\r\n")
def stringread(self,ss):
string = ""
while 1:
response = ss.recv(1)
if response != "\n":
string = string+""+response
else:
break
return string+"\n"
if __name__ == "__main__":
server = webserver(80,'/') # Make sure port is passed as integer
server.start() # '()' again
_______________________________________________
ActivePython mailing list
ActivePython@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython
Thread:
Jeremy Aiyadurai
Markus Daniel
|