简体   繁体   English

2 个不同网络上的 Python 2.7 套接字连接?

[英]Python 2.7 Socket connection on 2 different networks?

Title says all.标题说明了一切。 I have a client and a server setup, but they only work with localhost.我有一个客户端和一个服务器设置,但它们只适用于本地主机。 How can I connect to the socket from a different network?如何从不同的网络连接到套接字?

Client客户

# Echo client program
import socket
print "Client"
HOST = "localhost"               # Symbolic name meaning all available interfaces
PORT = 5001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST,PORT))
while True:
    data2 = raw_input()
    s.sendall(data2)

Server服务器

# Echo server program
import socket
print "Server"
HOST = ""           # Symbolic name meaning all available interfaces
PORT = 5001              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    print data
    if data == "Ping":
        print "Pong!"

conn.close()
  1. Check your public ip address using online tools like http://whatismyipaddress.com/使用在线工具(如http://whatismyipaddress.com/)检查您的公共 IP 地址
  2. Check your local ip address using ipconfig -a in windows ifconfig in linux.在 linux 的 windows ifconfig 中使用 ipconfig -a 检查您的本地 ip 地址。

Now if you are behind a dsl router (generally, you are) these addresses are completely different.现在,如果您在 dsl 路由器后面(通常是您),这些地址就完全不同了。 So you have to tell your router to send whenever a connection attemp received to a TCP port XXXX , forward it to "My Machine" (called Port Forwarding ).因此,您必须告诉您的路由器在收到 TCP 端口 XXXX 的连接尝试时发送,将其转发到“我的机器”(称为端口转发)。 Although port forwarding settings are similar in most routers, there is no standard (Generally under NAT / Port Forwarding menu items on your router web configuration interface ).尽管大多数路由器的端口转发设置都相似,但没有标准(通常在路由器 Web 配置界面上的 NAT/端口转发菜单项下)。 You may have to search instructions for your specific model.It's a good idea to set your computer to use a static ip address before port forwarding.Otherwise, the settings will be invalid if your computer is assigned another IP adress via DHCP.您可能需要搜索特定型号的说明。在端口转发之前将您的计算机设置为使用静态 IP 地址是个好主意。否则,如果您的计算机通过 DHCP 分配了另一个 IP 地址,则设置将无效。

If port forwarding is successful, now you only have to set your client application to connect to your public ip address.如果端口转发成功,现在您只需将客户端应用程序设置为连接到您的公共 IP 地址。 In your specific situation it's HOST = "XXXX" in your client source code.在您的特定情况下,您的客户端源代码中的 HOST = "XXXX" 。 Check if port forwarding works with a socket tester application you downloaded from somewhere.检查端口转发是否适用于您从某处下载的套接字测试器应用程序。 ( Don't test it with your experimental code, use an application you are sure that it's working). (不要用你的实验代码测试它,使用你确定它工作的应用程序)。 All did not work, check out the note below.一切都不起作用,请查看下面的注释。 It's the last resort ,though.不过,这是最后的手段。

Note : Some ISP's put their clients behind an extra firewall for security.注意:为了安全起见,一些 ISP 将他们的客户置于额外的防火墙之后。 A simple method to detect if this is the situation is , your Wan ip address you see in your router web interface will be different from what you see in online tools like whatsmyip.检测是否是这种情况的一种简单方法是,您在路由器 Web 界面中看到的 Wan ip 地址将与您在 whatsmyip 等在线工具中看到的不同。 In this situation no matter what you do , you will not be able to connect.在这种情况下,无论您做什么,都将无法连接。 You have to call your ISP and tell them to disable firewall for your connection .您必须致电您的 ISP 并告诉他们为您的连接禁用防火墙。 You may have some difficulties to explain them what you are talking about :).您可能很难向他们解释您在说什么:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM