gpt4 book ai didi

python - 远程连接到一个简单的套接字python服务器

转载 作者:可可西里 更新时间:2023-11-01 02:33:57 25 4
gpt4 key购买 nike

我正在尝试设置一个非常简单的套接字应用程序。我的服务器代码是:

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()

我将这个文件放在我的远程 Linode 服务器上,并使用 python server.py 运行它。我已经使用 nap 检查端口是否打开:

PORT     STATE SERVICE
22/tcp open ssh
80/tcp open http
1234/tcp open hotline

我现在在本地机器上运行 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

但是我没有收到任何类型的连接事件或报告。有人可以给我一些指示,说明我可能需要做什么吗?我是否需要在 client.py 中指定的 IP 地址中包含主机名?任何帮助将非常感激!

最佳答案

我刚刚总结了我们的评论,所以你的问题是这样的:

When you trying to using the client program connect to the server via the Internet, not 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.

关于python - 远程连接到一个简单的套接字python服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32563845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com