Matt Bone avatar Matt Bone committed ad6d677

server is configurable from the command line

Comments (0)

Files changed (2)

 if __name__ == '__main__':
 
     
-    with MemoServer("mbone-desktop", 8889) as memo_server:
+    with MemoServer("localhost", 9999) as memo_server:
         memo_server.put("asdf", "jkl")
         memo_server.put("qwer", "123")
 
 import threading
 import pickle
 
+DEFAULT_HOSTNAME = '0.0.0.0'
+DEFAULT_PORT = 9999
+
 RECV_SIZE = 1024
 
 PUT = 'p'
         self.clientsocket.send(pickled)
         
 
-def serve(port=8888, memo_queue=dict()):
+def serve(hostname=DEFAULT_HOSTNAME, port=DEFAULT_PORT, memo_queue=dict()):
     #originally from http://docs.python.org/dev/howto/sockets.html
     
     serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
     #bind to a host and port
-    print("binding to %s" % socket.gethostname())
-    serversocket.bind((socket.gethostname(), port))
+    serversocket.bind((hostname, port))
 
     #become a server socket
     serversocket.listen(5)
 
 
 if __name__ == '__main__':
+    from optparse import OptionParser
+    
+    parser = OptionParser()
+    parser.add_option("-p", "--port", type="int", dest="port", default=DEFAULT_PORT)
+    parser.add_option("--hostname", type="string", dest="hostname", default=DEFAULT_HOSTNAME)
+    (options, args) = parser.parse_args()
+
     memo_queue = dict()
     try:
-        serve(8889, memo_queue)
+        serve(hostname=options.hostname, port=options.port, memo_queue=memo_queue)
     except KeyboardInterrupt:
         print(memo_queue)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.