[英]Connecting to a simple sockets python server remotely
I am trying to setup a very simply sockets app. 我正在尝试设置一个非常简单的套接字应用程序。 My server code is:
我的服务器代码是:
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host,port))
s.listen(5) #Here we wait for a client connection
while True:
c, addr = s.accept()
print "Got a connection from: ", addr
c.send("Thanks for connecting")
c.close()
I placed this file on my remote Linode server and run it using python server.py
. 我将此文件放在我的远程Linode服务器上并使用
python server.py
运行它。 I have checked that the port is open using nap
: 我已经检查过使用
nap
打开端口:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
1234/tcp open hotline
I now run the client.py on my local machine: 我现在在本地机器上运行client.py:
import socket # Import socket module
s = socket.socket() # Create a socket object
port = 1234 # Reserve a port for your service.
s.connect(("139.xxx.xx.xx", port))
print s.recv(1024)
s.close # Close the socket when done
However I am not getting any kind of activity or report of connection. 但是,我没有得到任何形式的活动或连接报告。 Could someone give me some pointers to what I might have to do?
有人可以给我一些指示我可能要做的事吗? Do I need to include the hostname in the IP address I specify in the client.py?
我是否需要在client.py中指定的IP地址中包含主机名? Any help would be really appreciated!
任何帮助将非常感激!
I've just summarize our comments, so your problem is this: 我只是总结了我们的意见,所以你的问题是这样的:
When you trying to using the client program connect to the server via the Internet, not LAN.
当您尝试使用客户端程序通过Internet连接到服务器,而不是LAN。 You should configure the port mapping on your router.
您应该在路由器上配置端口映射 。
And however, you just need configure the port mapping for your server machine.
但是,您只需要为服务器计算机配置端口映射 。
After you did that, then you can use the client program connect to your server prigram.完成后,您可以使用客户端程序连接到您的服务器prigram。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.