[Twisted-Python] IP address, server

Brian Warner warner at lothar.com
Mon Jun 23 19:01:20 EDT 2003

Erik van Blokland <erik at letterror.com> writes:

> The tiny http server script works when I take a browser on
> the same machine as the server and look at
>, I get the expected result, so the
> server is alive. When I take a look from a different machine
> using the local IP address of the server
> I get no response. Other services over my network to the
> server work fine. Do I need to do more, listen to a specific
> port, tell the server the IP address? Is this supposed to
> work and the problem lies elsewhere, OS perhaps (would be
> good to know too)

Very strange. There's nothing in your code that should treat requests coming
from the local host any differently than requests coming in over the
network. My suspicion is that there's some kind of packet filter in place
that is dropping inbound connections to low-numbered ports.

You say other services on that same machine work fine, so it doesn't sound
like a network problem, or a filter that rejects *all* incoming connection
requests. Try having your Twisted server listen on a port like 9999 and see
what happens. Port 801 falls into the "privileged" range, so on many systems
you must be root to bind to it.. maybe OS-X does something special with
these kinds of ports.

I ran your script (on an older box, with python 2.2.0), and didn't see the
behavior you describe. (I did have to run it as root, and of course with
2.2.0 I had to turn True into 1). I do see a few other problems, though.
render() is supposed to either return a string with the HTML (or whatever)
to be sent to the client, *or* return NOT_DONE_YET and do .write/.finish .
Your code does .write, .finish, and then returns None, which isn't quite the
same (it calls .finish twice). I think it's ok to call .finish before you
return NOT_DONE_YET. I would write the code one of these two ways:

    def render(self, httprequest):
        hdr = httprequest.getAllHeaders()
        html = '<html><body>Have a nice day</body></html>'
        httprequest.setHeader('Content-Length', len(html))
        httprequest.setHeader('Content-Type', 'html/plain')
        return server.NOT_DONE_YET


    def render(self, httprequest):
        return '<html><body>Have a nice day</body></html>'

Note also that you're "html" variable has a %d that isn't currently being
used in a % format operation, so it will wind up as a literal % in the data
sent to the client.

You might want to add the following to the beginning of your code to see the
double-.finish warning and other log messages:

 from twisted.python import log
 import sys

Hope that gives you some new directions to try out..


More information about the Twisted-Python mailing list