#! /usr/bin/python
from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import time,threading
starttime = time.time()
class RequestHandler(BaseHTTPRequestHandler):
"""Definition of the request handler."""
def _writeheaders(self,doc):
"""Write the HTTP headers for the document. If there's no document,sen a 404 error code;
otherwise,send a 200 success code."""
if doc is None:
self.send_response(404)
else:
self.send_response(200)
# Always serve up HTML for now.
self.send_header('Content-type','text/html')
self.end_headers()
def _getdoc(self,filename):
"""Handle a request for a document,returning one of two different pages as appropriate."""
global starttime
if filename == '/':
return """
Sample Page This is a sample page. You can also look at the
Server Statistics
"""
elif filename == '/stats.html':
return """
Statistics This server has been runing for %d second.
""" % int(time.time() - starttime)
else:
return None
def do_HEAD(self):
"""Handle a request for headers only"""
doc = self._getdoc(self.path)
self._writeheaders(doc)
def do_GET(self):
"""Handle a request for headers and body"""
print "Handling with thread",threading.currentThread().getName()
doc = self._getdoc(self.path)
self._writeheaders(doc)
if doc is None:
self.wfile.write("""
Not Found The requested document '%s' was not found.
""" % self.path)
else:
self.wfile.write(doc)
class ThreadingHTTPServer(ThreadingMixIn,HTTPServer):
pass
serveraddr = ('',8765)
srvr = ThreadingHTTPServer(serveraddr,RequestHandler)
srvr.serve_forever()
|
文件: |
server.rar |
大小: |
0KB |
下载: |
下载 | |
阅读(1411) | 评论(0) | 转发(0) |