[Twisted-Python] IP address, server
Hello, a humble question for a nudge in the right direction. I'm utterly new to twisted, but I have a lot of experience in python. I'm trying to get a tiny working demo to play with and learn. I've installed twisted 1.0.5 on a Mac G3 running 10.2.6 OSX. Python 2.3b1. runtests seems to work. The tiny http server script works when I take a browser on the same machine as the server and look at http://127.0.0.1:801, 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 http://10.0.0.5:801 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) This is my test script: from twisted.web import resource from twisted.web import server from twisted.internet import reactor HTTP_PORT = 801 class MyServer(resource.Resource): isLeaf = True def render(self, httprequest): hdr = httprequest.getAllHeaders() html = '<html><body>Have a nice day %d</body></html>' httprequest.write(html) httprequest.setHeader('Content-Length', len(html)) httprequest.setHeader('Content-Type', 'html/plain') httprequest.setResponseCode(200) httprequest.finish() aserver = MyServer() site = server.Site(aserver) reactor.listenTCP(HTTP_PORT, site) reactor.run() Any pointers would be appreciated. Sorry if this is a dumb question.. Thanks, Erik van Blokland
Erik van Blokland <erik@letterror.com> writes:
The tiny http server script works when I take a browser on the same machine as the server and look at http://127.0.0.1:801, 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 http://10.0.0.5:801 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.write(html) httprequest.setHeader('Content-Length', len(html)) httprequest.setHeader('Content-Type', 'html/plain') httprequest.setResponseCode(200) return server.NOT_DONE_YET or: 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 log.startLogging(sys.stdout) Hope that gives you some new directions to try out.. cheers, -Brian
participants (2)
-
Brian Warner
-
Erik van Blokland