start a multi-sockets server (a socket/per thread) with different ports but same host

zxo102 zxo102 at gmail.com
Sun Aug 13 00:59:20 EDT 2006


Jean-Paul,
    I just start to learn Twisted. Here is my simple case: I can find
the data sent by clients in dataReceived but I don't know which
client/which port the data is from. After I know where the data comes
from, I can do different things there, for example, write them into
different files via bsddb.  I am not sure if it is the correct way to
do it.


   def dataReceived(self, data):
       # Accumulate the new data in our list
       self.received.append(data)
       # And then echo the entire list so far back to the client
       self.transport.write(''.join(data))

       print "============> data: ", data
       print " which Port? : ", self.factory.port  # unforunately it is
an object here.

       # if Port == 2001:
       #       write the data into a file via bsddb
       # if Port == 2002:
       #       write the data into another file via bsddb
       #  etc .....


Ouyang

Jean-Paul Calderone 写道:

> On 12 Aug 2006 10:44:29 -0700, zxo102 <zxo102 at gmail.com> wrote:
> >Jean-Paul,
> >Thanks a lot. The code is working. The python twisted is new to me too.
> >Here are my three more questions:
> >1. Since the code need to be started in a wxpyhon GUI (either by
> >clicking a button or up with the GUI),  do I have to run the code in a
> >thread (sorry, I have not tried it yet)?
>
> You can try to use Twisted's wxPython integration support:
>
> http://twistedmatrix.com/projects/core/documentation/howto/choosing-reactor.html#auto15
>
> But note the warnings about how well it is likely to work.  Using a separate
> thread might be the best solution.
>
> >2. How can I grab the client data in the code? Can you write two lines
> >for that? I really appreciate that.
>
> I'm not sure what you mean.  The data is available in the `received' attribute
> of the protocol instance.  Any code which needs to manipulate the data can get
> that list and do whatever it likes with it.
>
> >3. After I change
> >self.transport.write(''.join(self.data))
> >   to
> >self.transport.write(''.join(data))
> >  and scan all the ports with the following code twice (run twice).
> >First round scanning says "succefully connected". But second round
> >scanning says "failed". I have to restart your demo code to make it
> >work.
>
> I intentionally added code which shuts the server off after the first round
> of connections is completed, since that seemed to be what your example
> program was doing.  If you don't want this, just remove the shutdown code.
> 
> Jean-Paul




More information about the Python-list mailing list