Serve_forever??

Fredrik Lundh effbot at telia.com
Mon May 15 15:03:48 EDT 2000


A[r]TA <arta at x-stream.nl> wrote:
> Why doesn't this work??
> 
>     from SocketServer import *
> 
>     class request:
>         print 'blah'
> 
>     TCPServer(('127.0.0.1' , 80), request)
>     TCPServer.serve_forever()
> 
>     Traceback (innermost last):
>       File "<string>", line 1, in ?
>       File "C:\Program Files\Python\test.py", line 7, in ?
>         TCPServer.serve_forever()
>     TypeError: unbound method must be called with class instance 1st
> argument
> 
> I don't get it. It must be something simple, but can't figure out what.

you're calling serve_forever on the class, not the instance.  and the
request handler must be subclassed from a RequestHandler base class.

here's an example:

# adapted from socketserver-example-1.py in
# the eff-bot guide to the python standard library
# http://www.pythonware.com/people/fredrik/librarybook.htm

import SocketServer

# some user-accessible port
PORT = 8037

class myRequestHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        self.wfile.write("blah")

server = SocketServer.TCPServer(("", PORT), myRequestHandler)
print "listening on port", PORT
server.serve_forever()

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->




More information about the Python-list mailing list