Chinaunix首页 | 论坛 | 博客
  • 博客访问: 978311
  • 博文数量: 184
  • 博客积分: 10030
  • 博客等级: 上将
  • 技术积分: 1532
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-27 18:32
文章分类

全部博文(184)

文章存档

2009年(1)

2008年(63)

2007年(39)

2006年(79)

2005年(2)

我的朋友

分类: Python/Ruby

2008-03-09 16:53:33

#! /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) |
给主人留下些什么吧!~~