Opening sockets on multiple ports
Alain Désilets
alain.desilets at nrc.ca
Thu Nov 13 04:52:20 CET 2014
Found what the problem was.
Basically, the client could only connect to the TO_SERVER port, because that was the first port that the server listened to using the
SocketServer.serve_forever() method. For some reason, when I later invoked SocketServer.server_forever() on the FROM_SERVER port, it never got
to listen to that port, eventhough it was running in a separate thread from the first one.
I get around the problem by not using server_forever. Instead, I alternate between the two SocketServer instances and manually invoke
handle_request to handle a single request on each.
Attached is the modified code.
--
Alain Désilets
Agent de recherche
Conseil National de Recherches du Canada
Research Officer
National Research Council of Canad
-------------- next part --------------
import SocketServer
import threading
import sys
port_nums = {'toserver': 50007, 'fromserver': 50008}
#
# Handles a new connection on a given port
#
class ConnectionHandler(SocketServer.BaseRequestHandler):
def handle(self):
f = self.request.makefile()
print '... Connection %s: Starting handler' % (self.request.__dict__)
#
# This thread listens for new connections on a given port
#
class TwoWayConnectionServer(threading.Thread):
def __init__(self):
self.to_server = SocketServer.ThreadingTCPServer(("", port_nums['toserver']), ConnectionHandler)
self.from_server = SocketServer.ThreadingTCPServer(("", port_nums['fromserver']), ConnectionHandler)
threading.Thread.__init__(self)
def run(self):
#
# Alternate between the two servers
#
while (1):
self.to_server.handle_request()
self.from_server.handle_request()
#
# Start 2 threads, each listening for new connections on the TO_SERVER and
# FROM_SERVER ports respectively
#
def serve_non_blocking():
a_server = TwoWayConnectionServer()
a_server.run()
#
# This third thread simulates a busy server
#
class DoStuffThread(threading.Thread):
def run(self):
"""Simulate a very busy server"""
while 1:
x = 1
# print 'x=%s' % x
if __name__ == '__main__':
# Simulate busy server
doer = DoStuffThread()
doer.start()
# Listen for connections on the TO_SERVER and FROM_SERVER ports
serve_non_blocking()
-------------- next part --------------
from socket import *
import threading
import sys
# Port for transactions initiated by client
TO_SERVER_PORT = 50007
# Port for transactions initiated by server
FROM_SERVER_PORT = 50008
HOST = gethostname()
#
# This thread opens a new connection on the server's TO_SERVER port
#
class ToServerThread(threading.Thread):
def __init__(self):
print '-- ToServerThread: AF_INET=%s, SOCK_STREAM=%s, HOST=%s, TO_SERVER_PORT=%s' % (AF_INET, SOCK_STREAM, HOST, TO_SERVER_PORT)
aSocket = socket(AF_INET, SOCK_STREAM)
aSocket.connect(HOST, TO_SERVER_PORT)
print '-- TO_SERVER Connection %s opened' % repr(aSocket.__dict__)
def run(self):
pass
class FromServerThread(threading.Thread):
def __init__(self):
print '-- FromServerThread: AF_INET=%s, SOCK_STREAM=%s, HOST=%s, FROM_SERVER_PORT=%s' % (AF_INET, SOCK_STREAM, HOST, FROM_SERVER_PORT)
aSocket = socket(AF_INET, SOCK_STREAM)
aSocket.connect(HOST, FROM_SERVER_PORT)
print '-- FROM_SERVER connection %s opened' % repr(aSocket.__dict__)
def run(self):
pass
if __name__ == "__main__":
#
# Initiate connection to the server
#
if len(sys.argv) <= 1 or sys.argv[1] == 'toserver':
tsChannel = ToServerThread()
tsChannel.run()
else:
fsChannel = FromServerThread()
fsChannel.run()
#
# In the mean time, do some stuff in the main thread to simulate a busy
# client.
#
ii = 0
while 1:
# print "I'm a busy client: %s" % ii
ii = ii + 1
More information about the Python-list
mailing list