debuggers.hg

view tools/python/xen/web/httpserver.py @ 4655:a838a908e38e

bitkeeper revision 1.1327.2.2 (4267a9b3MhPpljnjQ5IbfLdzcW2K3w)

Remove twisted from the HTTP server and replace with a
threaded server. Add classes to provide tcp and unix servers
using threads instead of twisted. Remove use of twisted from
the consoles, event server and HTTP resources

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Thu Apr 21 13:25:07 2005 +0000 (2005-04-21)
parents
children d781b9d08e80
line source
1 import string
2 import socket
3 from urllib import quote, unquote
5 import http
6 from SrvDir import SrvDir
8 class HttpServerRequest(http.HttpRequest):
10 def __init__(self, server, addr, srd, srw):
11 #print 'HttpServerRequest>', addr
12 self.server = server
13 self.prepath = ''
14 http.HttpRequest.__init__(self, addr, srd, srw)
16 def process(self):
17 #print 'HttpServerRequest>process', 'path=', self.request_path
18 self.prepath = []
19 self.postpath = map(unquote, string.split(self.request_path[1:], '/'))
20 res = self.getResource()
21 self.render(res)
22 self.sendResponse()
23 return self.close_connection
25 def prePathURL(self):
26 url_host = self.getRequestHostname()
27 port = self.getPort()
28 if self.isSecure():
29 url_proto = "https"
30 default_port = 443
31 else:
32 url_proto = "http"
33 default_port = 80
34 if port != default_port:
35 url_host += (':%d' % port)
36 url_path = quote(string.join(self.prepath, '/'))
37 return ('%s://%s/%s' % (url_proto, url_host, url_path))
39 def getResource(self):
40 return self.server.getResource(self)
42 def render(self, res):
43 #print 'HttpServerRequest>render', res
44 if res is None:
45 self.sendError(http.NOT_FOUND)
46 else:
47 res.render(self)
49 class HttpServer:
51 request_queue_size = 5
53 def __init__(self, interface='', port=8080, root=None):
54 if root is None:
55 root = SrvDir()
56 self.interface = interface
57 self.port = port
58 self.closed = False
59 self.root = root
61 def getRoot(self):
62 return self.root
64 def getPort(self):
65 return self.port
67 def run(self):
68 self.bind()
69 self.listen()
70 self.requestLoop()
72 def stop(self):
73 self.close()
75 def bind(self):
76 #print 'bind>', self.interface, self.port
77 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
78 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
79 self.socket.bind((self.interface, self.port))
81 def listen(self):
82 self.socket.listen(self.request_queue_size)
84 def accept(self):
85 return self.socket.accept()
87 def requestLoop(self):
88 while not self.closed:
89 self.acceptRequest()
91 def close(self):
92 self.closed = True
93 try:
94 self.socket.close()
95 except:
96 pass
98 def acceptRequest(self):
99 #print 'acceptRequest>'
100 try:
101 (sock, addr) = self.accept()
102 #print 'acceptRequest>', sock, addr
103 self.processRequest(sock, addr)
104 except socket.error:
105 return
107 def processRequest(self, sock, addr):
108 #print 'processRequest>', sock, addr
109 srd = sock.makefile('rb')
110 srw = sock.makefile('wb')
111 srvaddr = (socket.gethostname(), self.port)
112 while True:
113 #print 'HttpServerRequest...'
114 req = HttpServerRequest(self, srvaddr, srd, srw)
115 close = req.process()
116 srw.flush()
117 #print 'HttpServerRequest close=', close
118 if close:
119 break
120 try:
121 #print 'close...'
122 sock.close()
123 except:
124 pass
125 #print 'processRequest<', sock, addr
127 def getResource(self, req):
128 return self.root.getRequestResource(req)
131 def main():
132 root = SrvDir()
133 a = root.add("a", SrvDir())
134 b = root.add("b", SrvDir())
135 server = HttpServer(root=root)
136 server.run()
138 if __name__ == "__main__":
139 main()