How should I handle socket receiving?

Hans hansyin at gmail.com
Mon Mar 14 15:47:03 EDT 2011


On Mar 12, 10:13 pm, Tim Roberts <t... at probo.com> wrote:
> Hans <hans... at gmail.com> wrote:
>
> >I'm thinking to write a code which to:
> >1. establish tons of udp/tcp connections to a server
>
> What does "tons" mean?  Tens?  Hundreds?
>
> >my question is how should I handle receiving traffic from each
> >connection respectively?
>
> You're really going to want to use "select".  You can store the objects in
> a dictionary where the key is the socket number.  That way, you can use the
> result of the select and get your network object directly.
> --
> Tim Roberts, t... at probo.com
> Providenza & Boekelheide, Inc.

I wrote code like this:
main proc:
import socket_thread
#start 1000 connection
while i<1000:
    my_socket=socket_thread.socket_thread(i,host,port)
    my_socket.send(some_data)
    my_socket.recv()

socket_thread.py
class socket_thread:
    def __init__:
          self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
    def send(data):
          self.soc_handle.send(data)
    def recv():
          while 1:
 
input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
2)
              data=input_list[0].recv(2048)
              print data

But it does not work as I hope. main proc can only initiate one thread
and then trapped by it, cannot get out.
I'm sure I missed something but I don't know. Thanks for any help.



More information about the Python-list mailing list