October 28th, 2002, 12:15 PM
Multiple Socket Connections - How-to?
Ok, heres the deal. I have a flatfile with a list of proxy address' and ports, they take the form of serverort
Now, what I havew is some code to readlines() the text file and split it into two values to be used.
Now, For every entry in this list, I want to open a socket connection to the proxy server to see if it's alive. This is how far I have gotten with my code.
What seems to be happening is sockets are being opened, but blocked. I've looked everywhere on decent docs to help me through this, but I've no luck.
I'm guessing I need to use select() to move through each connection. I want to be able to simultaneously, or close to simultaneously, open up several connections on seperate sockets.
I thought that creating a new socket for each loop through, would allow me to do this, but nope, it doesn't. I even tried taking the server/port values, sticking them in a list, then looping through each of them.
Anyone who could help me out at all would be a savior.
print "Preparing to connect..."
for self.entry in self.proxylist:
# set string seperator
self.dlm = re.compile(r':')
#split string by seperator
self.lst = self.dlm.split(self.entry)
# create new proxy object
self.po = proxy_object()
# set up proxy_object socket
self.po.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set proxy_object to non-blocking socket
# set proxy_object name
self.po.name = self.lst
# set proxy_object server
self.po.serv = self.lst
# set proxy_object port
self.po.port = int(self.lst)
# append proxy_object to proxygroup list
self.proxyobject = self.po
# Begin connecting to all box' in the proxygroup
for box in proxygroup:
print self.po.conn, "connecting to", self.po.name, "on port", self.po.port
rr, rw, ie = select.select([box.conn], [box.conn], [box.conn], 3)
except socket.error, msg:
print "Error", msg
print rr, rw, ie
November 7th, 2002, 01:04 PM
November 8th, 2002, 12:36 AM
Aye, I've tried asyncore.
That works to an extent; handling connections, sending requests etc. but it seems to pipeline them.
E.g. If I place the asyncore.loop() inside a for loop, for each socket connection thats made, it makes, the asyncore.loop handles the select routine (I guess), but blocks the next connection to be made in the loop until the previous one is complete.
If I move the asyncore.loop() outside of the for loop, all the connections are made first and foremost, then the asyncore.loop() is called and checks for writeable sockets, if writeable, the request is sent off. Now, while that request is sent off, if the proxy is latent, then the next request isn't sent off.
Mind you, I could be wrong and it's just looping through all the socket connection to see which are ready to send requests and receive them. Would that be the case?
I think thats the meat of my question: when using asyncore.loop(), does it loop through all socket connections and check for r, w and except, then handle it? E.g. If a socket is ready to write to, it will send whatever request via the socket then move on in the loop looking for the next socket thats ready to be used. If it does that, then does it block while sending (assuming the server is latent)?