Need help with httplib
Carl Waldbieser
waldbie at attglobal.net
Sun Aug 18 03:49:51 EDT 2002
I am trying to create a simple web server that forwards GET, HEAD, and POST
requests to a different server and then sends the response back to the
original client. I am using the BaseHTTPServer and httplib modules to try
and do this. I almost have it working correctly except for a problem with
the POST. The request data is successfully POSTed, but when the (real) web
server responds to my redirecting web server, it sends back a status of 100,
Continue.
I am no HTTP expert, but I tried digging through some RFCs to try and figure
this one out. As far as I can tell, the web server is not suposed to send
this status back unless I sent an "Expect" request header of "100-continue",
which the client does not send.
If anyone can explain to me what is going on here, I'd appreciate it.
My set-up is as follows:
Everything runs locally on a single Win98 box.
The main web server is PWS (Personal Web Server) on port 80.
The Python redirecting server listens to port 8000.
I point my web browser (IE6.0) at http://localhost:8000/some_local_url.html
and it the redirecter forwards the request to
http://localhost/some_local_url.html. The response is then returned to the
client.
This works fine for GET. POST seems to be giving me a problem as it
responds with the 100 status. Should it be doing this?
Thanks for any help,
Carl Waldbieser
waldbie at attglobal.net
Python code below:
################################
import BaseHTTPServer
import httplib
import socket
g_RemoteServer = "localhost"
class HTTPRedirector(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
srcfile = self.send_headers("GET")
if srcfile:
BLOCKSIZE = 8192
while 1:
data = srcfile.read(BLOCKSIZE)
if not data:
break
self.wfile.write(data)
srcfile.close()
def do_POST(self):
srcfile = self.send_headers("POST")
if srcfile:
BLOCKSIZE = 8192
while 1:
data = srcfile.read(BLOCKSIZE)
if not data:
break
self.wfile.write(data)
srcfile.close()
def do_HEAD(self):
srcfile = self.send_headers("HEAD")
if srcfile:
srcfile.close()
def send_headers(self, request):
print "Client request version: %s" % self.request_version
try:
conn = httplib.HTTPConnection(g_RemoteServer)
except httplib.HTTPException:
print "HTTP error-- cannot connect to %s." % g_RemoteServer
return
conn.connect()
conn.putrequest(request, self.path)
print "--Client Headers--"
for header, val in self.headers.items():
conn.putheader(header, val)
if header.lower() == "content-length":
size = int(val)
print "%s : %s" % (header, val)
conn.endheaders()
if request == "POST":
data = self.rfile.read(size)
conn.send(data)
print "Sent %d bytes of data." % len(data)
print data
response = conn.getresponse()
self.send_response(response.status, response.reason)
print "Response status: %s, reason: %s" % (response.status,
response.reason)
print "--Response Headers--"
for header in response.msg.headers:
pos = header.find(":")
self.send_header(header[:pos], header[pos+1:-1])
print "%s : %s" %(header[:pos], header[pos+1:-1])
self.end_headers()
if response.status == 200:
return response
if __name__ == "__main__":
print "Redirecting HTTP requests to", g_RemoteServer
BaseHTTPServer.test(HTTPRedirector)
More information about the Python-list
mailing list