Question

I wrote a simple python script using the SocketServer, it works well on Windows, but when I execute it on a remote Linux machine(Ubuntu), it doesn't work at all.. The script is like below:

#-*-coding:utf-8-*- 
import SocketServer

class MyHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        data_rcv = self.request.recv(1024).strip()
        print data_rcv

myServer = SocketServer.ThreadingTCPServer(('127.0.0.1', 7777), MyHandler)   
myServer.serve_forever()

I upload it to the remote machine by SSH, and then run the command python server.py on the remote machine, and try to access to xxx.xxx.xxx.xxx:7777/test with my browser, but nothing is printed on the remote machine's teminal...any ideas?

UPDATE: Problem solved, it's a firewall issue, thanks you all.

Was it helpful?

Solution

You are binding the server to 127.0.0.1, the IP address for localhost. This means the server will only accept connections originating from the same machine; it won't recognize ones coming from another machine.

You need to either bind to your external IP address, or bind to a wildcard address (i.e. don't bind to any particular IP address, just a port). Try:

myServer = SocketServer.ThreadingTCPServer(('0.0.0.0', 7777), MyHandler) 

OTHER TIPS

You are binding to 127.0.0.1:7777 but then trying to access it through the servers external IP (I'll use your placeholder - xxx.xxx.xxx.xxx). 127.0.0.1:7777 and xxx.xxx.xxx.xxx:7777 are different ports and can be bound by different processes IIRC.

If that doesn't fix it, check your firewall, many hosts set up firewalls that block everything but the handful you are likely to use

Try with telnet or nc first, telnet to your public ip with your port and see what response you get. Also, why are accessing /test from the browser? I don't see that part in the code. I hope you have taken care of that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top