Passing data into my StreamRequestHandler?

Andrew Bennetts andrew-pythonlist at puzzling.org
Tue Jun 3 15:11:51 CEST 2003


On Tue, Jun 03, 2003 at 01:37:58PM +0100, Richard wrote:
> Hi,
> 
> I have a TCP Server class using SocketServer.TCPServer( ) and a handler
> class which inherits from SocketServer.StreamRequestHandler. When an
> incoming connection is made, the overridden handle( ) function gets called
> which is fine, however can anyone tell me how I go about passing data into
> the handler object from my server object?

I don't remember enough about SocketServer to remember how to do exactly
what you asked, but....

> Here is my code:
> 
> import SocketServer
> 
> class ConnectionEndpointHandler(SocketServer.StreamRequestHandler):
> 
>  def handle(self):
>   print "Connected!"
> 
> class sServer:
> 
>  m_InAddress = []
>  m_OutAddress = []
> 
>  def __init__(self, inAddress):
>   self.m_InAddress = inAddress
> 
>  def start(self):
>   m_Server = SocketServer.TCPServer((self.m_InAddress[0],
> int(self.m_InAddress[1])), ConnectionEndpointHandler)
>   m_Server.serve_forever()

In Twisted, this looks like:

----
from twisted.internet.protocols import Protocol, ServerFactory
from twisted.internet import reactor

class MyProtocol(Protocol):
    def connectionMade(self):
        print "Connected!"
        # Write greeting defined in my factory
        self.transport.write(self.factory.greeting)

class MyFactory(ServerFactory):
    protocol = MyProtocol
    greeting = 'Homer says: "Welcome to the Internet!"\n'

def start(port):
    reactor.listenTCP(port, MyFactory())
    reactor.run()
----

In Twisted, you put a Factory on a port to listen for connections, and build
Protocol instances for each incoming connection, so (unless you're
intentionally doing something wacky) a Protocol instance corresponds to
exactly one connection.

[By default, a factory's buildProtocol handler is:

    def buildProtocol(self, addr):
        p = self.protocol()
        p.factory = self
        return p

so that protocols have access to their factory]

There are probably similar hooks you can use in SocketServer, but I'd
recommend Twisted anyway, as it's a much richer framework.

> Also, as it currently stands (I have another file which just creates an
> instance of my sServer, provides the inAddress and calls start( ) ) this
> just continually runs, printing 'Connected' every time I make a connection
> to it. I know that serve_forever( ) does this, but can anyone tell me how I
> interrupt this to exit the program? I am running on Win2K and CTRL-C doesn't
> seem to work, meaning I am forced to close down the console window.

On Win2K, Ctrl-C won't interrupt a call to select.select, and I expect this
(or something similar, like perhaps a blocking call to socket.accept) is
what is happening to you.  You may find Ctrl-Break works (so long as a
SIGBREAK handler hasn't been installed), or that hitting Ctrl-C then
connecting to your server will work (because this wakes up the select call).
You'll see the same problem if you run your code as an NT service using Mark
Hammond's win32all package, and try to stop the service.

Twisted on Win32 has a timeout on its select call (5 seconds, iirc) to
workaround this exact same problem.  I'm not sure if there's any easy way to
apply the same trick to SocketServer.

-Andrew.






More information about the Python-list mailing list