Commits

Victor Kotseruba committed 773f107

wonderfull map replaced with optparse

Comments (0)

Files changed (1)

 # -*- coding: utf-8 -*-
 
-import asynchat, asyncore, socket, sys
+import asynchat, asyncore, socket, optparse
 
-usage = """
-Usage:
-    $ python FlSock.py PORT CHANNELS MAX_MESSAGE_LENGTH 
 
-"""
-
+parser = optparse.OptionParser()
+parser.add_option('-i', dest='host', default='',
+                  help='address to listen, default all available')
+parser.add_option('-p', dest='port', default=8881,
+                  help='port, default 8881', type='int')
+parser.add_option('-c', dest='channels', default=100,
+                  help='max channels, default 100', type='int')
+parser.add_option('-m', dest='max_length', default=1024,
+                  help='max message length, default 1024', type='int')
+parser.add_option('-d', dest='domains', default='*',
+                  help='allowed domains comma separated, default *')
 
 class FlSockChannel(asynchat.async_chat):
 
     ac_in_buffer_size = 1024
     ac_out_buffer_size = 1024
 
-    def __init__(self, server, sock, max_length):
+    def __init__(self, server, sock, opts):
         asynchat.async_chat.__init__(self, sock)
         self.data = ''
         self.state = 1
-        self.max_length = max_length
+        self.opts = opts
         self.server = server
         self.flash_ready = True
         self.set_terminator(1)
     
     def collect_incoming_data(self, data):
         self.data = self.data + data
-        if len(self.data) > self.max_length:
+        if len(self.data) > self.opts.max_length:
             self.close()
     
     def found_terminator(self):
         if self.state == 1:
             if self.data == '<':
                 self.push('<?xml version="1.0"?>')
-                self.push('<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">')
+                self.push('<!DOCTYPE cross-domain-policy')
+                self.push('SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">')
                 self.push('<cross-domain-policy>')
-                self.push('<allow-access-from domain="*" to-ports="*" />')
+                for domain in self.opts.domains.split(','):
+                    self.push('<allow-access-from domain="%s" to-ports="%i" />' % (domain, self.opts.port))
                 self.push('</cross-domain-policy>')
                 self.close_when_done()
             elif self.data == 'm':
     
 class FlSockDispatcher(asyncore.dispatcher):
     
-    def __init__(self, port, channels, max_length):
+    def __init__(self, opts):
         asyncore.dispatcher.__init__(self)
-        self.max_length = max_length
+        self.opts = opts
         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
         self.set_reuse_addr()
-        self.bind(('', port))
-        self.listen(channels)
+        self.bind((opts.host, opts.port))
+        self.listen(opts.channels)
     
     def handle_accept(self):
         conn, addr = self.accept()
-        FlSockChannel(self, conn, self.max_length)
+        FlSockChannel(self, conn, self.opts)
 
 
 if __name__ == '__main__':
-    if len(sys.argv) != 4:
-        print usage
-        sys.exit(-1)
-    s = FlSockDispatcher(*map(int, sys.argv[1:]))
+    opts, args = parser.parse_args()
+    s = FlSockDispatcher(opts)
     asyncore.loop()