Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SocketServer doesn't work on Linux

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.

like image 990
wong2 Avatar asked Jul 12 '26 05:07

wong2


1 Answers

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) 
like image 187
John Kugelman Avatar answered Jul 14 '26 19:07

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!