Capture the request/response log for local web server through python.

S

shanti bhushan

Dear all,
I have made local webserver up by the python script

import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
#import pri
import glob
import logging
import logging.handlers
class MyHandler(BaseHTTPRequestHandler):

def do_GET(self):
try:
if self.path.endswith(".html"):
f = open(curdir + sep + self.path) #self.path has /
test.html
#note that this potentially makes every file on your computer readable
by the internet

self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
if self.path.endswith(".esp"): #our dynamic content
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write("hey, today is the" +
str(time.localtime()[7]))
self.wfile.write(" day in the year " +
str(time.localtime()[0]))
return

return

except IOError:
self.send_error(404,'File Not Found: %s' % self.path)


def do_POST(self):
global rootnode
try:
ctype, pdict =
cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query=cgi.parse_multipart(self.rfile, pdict)
self.send_response(301)

self.end_headers()
upfilecontent = query.get('upfile')
print "filecontent", upfilecontent[0]
self.wfile.write("<HTML>POST OK.<BR><BR>");
self.wfile.write(upfilecontent[0]);

except :
pass

def main():
try:
server = HTTPServer(('', 80), MyHandler)
print 'started httpserver...'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()

if __name__ == '__main__':
main()


I have designed one html page also.
when i access the HTML page ,i want to capture following things
user_agents client-request ,server-response with the help of python
script.
please guide me to write such python script with which i can log all
server /client request and response.

Regards,
Shanti Bhushan
(e-mail address removed)
 
G

Gabriel Genellina

En Tue, 15 Jun 2010 00:41:08 -0300, shanti bhushan
Dear all,
I have made local webserver up by the python script

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):

def do_GET(self):
try:
if self.path.endswith(".html"):
f = open(curdir + sep + self.path) #self.path has /
[...]

def main():
try:
server = HTTPServer(('', 80), MyHandler)
print 'started httpserver...'
server.serve_forever()
[...]


I have designed one html page also.
when i access the HTML page ,i want to capture following things
user_agents client-request ,server-response with the help of python
script.
please guide me to write such python script with which i can log all
server /client request and response.

HTTPServer already logs the request - using sys.stderr, but you may
override log_message() if you want:
http://docs.python.org/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler.log_message

If you want to log the response, do that in the request handler, a good
place would be at the end of your do_GET() method above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,441
Members
47,101
Latest member
DoloresHol

Latest Threads

Top