HttpServer help!
Eric Texier
erict at millfilm.co.uk
Thu Jun 6 07:48:36 EDT 2002
I am trying to get some simple example running locally on my
machine of http(server/client) code from the book 'Python & Xml'
and I am having trouble.
if someone can look at the following script and tell me if
the problem can be because I don't have the privilege and/or
I didn't setup my system properly:
Thanks.
I am running on Linux Red Hat with python2.2.
The client code to POST:
"""
post.py
"""
from httplib import HTTP
from urllib import quote
# establish POST data
myquote = 'This is my quote: "I think therefore I am."'
# be sure not to quote the key= sequence...
postdata = "favquote=" + quote(myquote)
print "Will POST ", len(postdata), "bytes:"
print postdata
# begin HTTP request
#req = HTTP("127.0.0.1") # change to your IP or localhost
#req = HTTP("localhost") # change to your IP or localhost
##### MY IP address
req = HTTP("10.16.3.132") # change to your IP or localhost
#req.putrequest("POST", "/c8/favquote.cgi")
req.putrequest("POST",
"file:/usr/people/erict/python/srcToolskit/pythonXmlBook/c8/favquote.cgi")
.
.
.
##### That the Error after the ruprequest->"#####
Traceback (most recent call last):
File "post.py", line 25, in ?
req.putrequest("POST",
"file:/usr/people/erict/python/srcToolskit/pythonXmlBook/c8/favquote.cgi")
File "/usr/people/erict/bin//lib/python2.2/httplib.py", line 453, in
putrequest
self.send(str)
File "/usr/people/erict/bin//lib/python2.2/httplib.py", line 395, in
send
self.connect()
File "/usr/people/erict/bin//lib/python2.2/httplib.py", line 379, in
connect
raise socket.error, msg
socket.error: (111, 'Connection refused')
The server seems to work fine: Here is the code: HTTPServer.py
##########################################################
"""
HTTPServer.py - an simple implementation
of the BaseHTTPServer module
"""
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
import string
"""
class myRequestHandler - Handles any and all request
coming in, regardless of path, file, or request
method (GET/POST)
"""
class myRequestHandler(BaseHTTPRequestHandler):
"""
do_GET will be called if the browser does a GET
request.
"""
def do_GET(self):
# give them back a 200 OK every time.
self.printCustomHTTPResponse(200)
# start HTML output
self.wfile.write("<html><body>")
self.wfile.write("<p>Hello, I am a web server, sort of.</p>")
self.wfile.write("<p>GET string: " + self.path + "</p>")
# show browser headers
self.printBrowserHeaders()
# finish off HTML
self.wfile.write("</html></body>")
"""
do_POST is called if the browser is POSTing data
from a form
"""
def do_POST(self):
# send back a 200 OK
self.printCustomHTTPResponse(200)
# start HTML and show browser headers again
self.wfile.write("<html><body>")
self.printBrowserHeaders()
self.wfile.write("<b>Post Data:</b><br>")
# track down length of the post, so that you
# can read in the correct number of bytes. The
# length of the post is in the browser header
# named 'content-length'.
if self.headers.dict.has_key("content-length"):
# convert content-length from string to int
content_length = string.atoi(self.headers.dict["content-length"])
# read in the correct number of bytes from the client
# connection and send it back to the browser
raw_post_data = self.rfile.read(content_length)
print("[Post Data:]", raw_post_data)
self.wfile.write(raw_post_data)
# finish off HTML
self.wfile.write("</html></body>")
"""
printBrowserHeaders - this method prints the HTTP
headers sent by the client
"""
def printBrowserHeaders(self):
# iterate through header dictionary and
# display the name and value of each pair.
self.wfile.write("<p>Headers: <br>")
header_keys = self.headers.dict.keys()
for key in header_keys:
self.wfile.write("<b>" + key + "</b>: ")
self.wfile.write(self.headers.dict[key] + "<br>")
"""
printCustomHTTPResponse - this method takes a response
code and sends the code and custom headers
back to the browser
"""
def printCustomHTTPResponse(self, respcode):
# send back appropriate HTTP code
self.send_response(respcode)
# tell them we're sending HTML
self.send_header("Content-type", "text/html")
# describe the server software in use!
self.send_header("Server", "myRequestHandler :-)")
# close off headers
self.end_headers()
# start the server on port 2112, requires browser URLs
# to be addressed as http://servername:2112/pathtofile
myServer = HTTPServer(('', 2112), myRequestHandler)
# loop to handle 5 requests
for lp in range(5):
myServer.handle_request()
##########################################################
More information about the Python-list
mailing list