Changeset 314 for py-tcpmultiplexer
- Timestamp:
- Jul 19, 2011, 2:11:09 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
py-tcpmultiplexer/TCPMultiplexer.py
r313 r314 15 15 import time 16 16 import urllib2 17 import os 17 18 18 19 # Some boring defaults … … 20 21 DEFAULT_PORT = 9999 21 22 DEFAULT_CONFIG = 'streams.yaml' 23 DEFAULT_DOCUMENTROOT = './htdocs' 24 22 25 23 26 # URL : TARGET … … 33 36 running = True 34 37 recv_threads = [] 38 document_root = None 35 39 36 40 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') … … 50 54 51 55 def handle(self): 52 global running, dataheader, urlheader, urldata56 global running, dataheader, document_root, urlheader, urldata 53 57 # self.request is the TCP socket connected to the client 54 58 self.data = self.request.recv(1024).strip() 55 print self.data56 59 urlid = self.data.split('\n')[0].split()[1] 57 60 req_type = self.data.split('\n')[0].split()[0] 61 # XXX: Check for .. paths 62 document_file = document_root + urlid 58 63 logging.info("Connection %s from '%s' for '%s'", req_type, self.client_address[0], urlid) 59 64 try: 60 if not urldata.has_key(urlid): 61 self.request.send("HTTP/1.1 404 NOT FOUND\n\nThe page '%s' does not exists" % urlid) 62 else: 65 if urlid == '/': 66 self.request.send("HTTP/1.1 200 OK\n\n<html><head><title>Overview</title></head><body><h3>Streams</h3><ul>") 67 for urlid in urldata.keys(): 68 self.request.send('<li><a href="%(urlid)s">%(urlid)s</a></li>' % {'urlid': urlid}) 69 self.request.send("</ul><h3>Files</h3><ul>") 70 for root, dirs, files in os.walk(document_root): 71 for name in files: 72 self.request.send('<li><a href="%(item)s">%(item)s</li>' % { 'item' : os.path.join(root.replace(document_root,'',1),name) }) 73 self.request.send("</ul></body></html>") 74 elif urldata.has_key(urlid): 63 75 self.request.send('HTTP/1.1 200 OK\n' + urlheader[urlid] + 'X-Proxy-Server: TCPMultiplexer.py\n' + '\n' + dataheader[urlid]) 64 76 if req_type == 'HEAD': … … 71 83 continue 72 84 self.request.send(urldata[urlid][self].pop(0)) 73 else: 74 assert False, "Invalid request" 85 elif os.path.isfile(document_file): 86 self.request.send('HTTP/1.1 200 OK\n\n' + open(document_file,'r').read()) 87 else: 88 self.request.send("HTTP/1.1 404 NOT FOUND\nContent-Type: text/html\n\n<h1>404</h1>The page '%s' does not exists" % urlid) 75 89 except IOError: 76 90 pass … … 129 143 parser.add_argument('--port', dest='port', default=DEFAULT_PORT, type=int, help='Listen to PORT [default: %s]' % DEFAULT_PORT) 130 144 parser.add_argument('--stream-cfg', dest='stream_cfg', default=DEFAULT_CONFIG, help='YAML Stream configuration [default: %s]' % DEFAULT_CONFIG) 145 parser.add_argument('--document-root', dest='document_root', default=DEFAULT_DOCUMENTROOT, help='Document Root File Directory [default: %s]' % DEFAULT_DOCUMENTROOT) 131 146 args = parser.parse_args() 147 148 document_root = os.path.abspath(os.path.join(os.path.dirname(__file__),args.document_root)) 149 logging.info("Serving '/htdocs' from document_root '%s'", document_root) 132 150 133 151 # Inport streams
Note:
See TracChangeset
for help on using the changeset viewer.