Commits

Matt Bone committed ad6d677

server is configurable from the command line

  • Participants
  • Parent commits 17677bb

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)