]> git.pld-linux.org Git - projects/pld-builder.new.git/blob - PLD_Builder/request_handler_server.py
- report startup bind error nicely (with host:port address)
[projects/pld-builder.new.git] / PLD_Builder / request_handler_server.py
1 #!/usr/bin/python
2
3 import socket
4 import string
5 import cgi
6 import time
7 import log
8 import sys
9 from config import config, init_conf
10
11 from os import curdir, sep
12 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
13
14 import request_handler
15
16 class MyHandler(BaseHTTPRequestHandler):
17
18         def do_GET(self):
19                 self.send_error(401);
20
21         def do_POST(self):
22                 global rootnode
23                 try:
24                         length = int(self.headers.getheader('content-length'))
25                         ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
26                         if ctype != 'application/x-www-form-urlencoded':
27                                 log.error("request_handler_server: [%s]: 401 Unauthorized" % self.client_address[0])
28                                 self.send_error(401)
29                                 self.end_headers()
30                                 return
31
32                         query = self.rfile.read(length)
33
34                         filename = self.headers.getheader('x-filename')
35
36                         if not request_handler.handle_request_main(query, filename = filename):
37                                 log.error("request_handler_server: [%s]: handle_request_main(..., %s) failed" % (self.client_address[0], filename))
38                                 self.send_error(500)
39                                 self.end_headers()
40                                 return
41
42                         self.send_response(200)
43                         self.end_headers()
44
45                 except Exception, e:
46                         self.send_error(500)
47                         self.end_headers()
48                         log.error("request_handler_server: [%s]: exception: %s" % (self.client_address[0], e))
49                         raise
50                         pass
51
52 def main():
53         socket.setdefaulttimeout(30)
54         try:
55                 init_conf()
56                 host = ""
57                 port = config.request_handler_server_port
58
59                 try:
60                         server = HTTPServer((host, port), MyHandler)
61                 except Exception, e:
62                         log.notice("request_handler_server: can't start server on [%s:%d]: %s" % (host, port, e))
63                         print >> sys.stderr, "ERROR: Can't start server on [%s:%d]: %s" % (host, port, e)
64                         sys.exit(1)
65
66                 log.notice('request_handler_server: started on [%s:%d]...' % (host, port))
67                 server.serve_forever()
68         except KeyboardInterrupt:
69                 log.notice('request_handler_server: ^C received, shutting down server')
70                 server.socket.close()
71
72 if __name__ == '__main__':
73         main()
74
This page took 0.13489 seconds and 4 git commands to generate.