简体   繁体   English

Python中通过互联网的套接字连接?

[英]Socket connection over internet in Python?

I have created a basic client server socket program in Python 2.7.x and it is running absolutely fine over the same network even on different machines but when I run server and client on different networks(server on my friend's network while client on mine) it does not return any error and keeps on waiting.我在 Python 2.7.x 中创建了一个基本的客户端服务器套接字程序,即使在不同的机器上,它也可以在同一网络上运行,但是当我在不同的网络上运行服务器和客户端时(服务器在我朋友的网络上,而客户端在我的网络上)它不返回任何错误并继续等待。 I just can't understand how to debug the code.我只是不明白如何调试代码。 I am using port 80 by killing all the services on port 80. I have also done port forwarding on port 80 on both the machines.我通过杀死端口 80 上的所有服务来使用端口 80。我还在两台机器上的端口 80 上进行了端口转发。

My codes is as follows:我的代码如下:

client.py客户端.py

import socket              

s = socket.socket()        
host = '103.47.59.130' 
port = 80               

s.connect((host, port))
while True: 
    print "From Server: ", s.recv(1024)  #This gets printed after sometime
    s.send(raw_input("Client please type: "))

s.close()                     

server.py服务器.py

import socket               

s = socket.socket()         # Create a socket object
host = '192.168.0.104'    #private ip address of machine running fedora
port = 80                
s.bind((host, port))       

s.listen(5)                
c, addr = s.accept()       
print 'Got connection from', addr    #this line never gets printed
while True:
   c.send(raw_input("Server please type: "))
   print "From Client: ", c.recv(1024)

c.close()                

It sometimes output **From Server: ** but doesnot send any message back and forth.它有时会输出 **From Server: ** 但不会来回发送任何消息。

PS: I have searched on Stack Overflow earlier but I am unable to find anything relevant. PS:我之前在 Stack Overflow 上搜索过,但找不到任何相关内容。

Use this software to implement port-forwarding.使用该软件实现端口转发。 I recommend you use another port for your server, say 5006, to prevent any problems related to using a very commonly used port like 80. Basically, the software works like this:我建议您为您的服务器使用另一个端口,例如 5006,以防止与使用非常常用的端口(如 80)相关的任何问题。基本上,该软件的工作原理如下:

  • You click Connect, and it searches for routers and if it finds yours, it lists the existing port mappings.您单击连接,它会搜索路由器,如果找到您的,它会列出现有的端口映射。
  • You create a port mapping (on the right), default protocol is TCP您创建一个端口映射(在右侧),默认协议是 TCP
  • You select a port on your router, say 5001 (called the External port)您在路由器上选择一个端口,例如 5001(称为外部端口)
  • You select a port on your server, maybe 5006 (called the Internal port)您在服务器上选择一个端口,可能是 5006(称为内部端口)
  • The router will then be instructed to forward all data that arrives at port 5001 to your server, using your private IP, specifically to port 5006 on your server.然后将指示路由器使用您的私有 IP 将到达端口 5001 的所有数据转发到您的服务器,特别是您服务器上的端口 5006。

So all your client has to do is send data to the public IP of your server, specifically to port 5001. This data will of course first arrive at your router, which will behave as configured and forward everything to your server's port 5006. All this only works if your internet gateway supports port forwarding.因此,您的客户端所要做的就是将数据发送到服务器的公共 IP,特别是端口 5001。这些数据当然会首先到达您的路由器,它将按照配置的方式运行并将所有内容转发到服务器的端口 5006。所有这些仅当您的互联网网关支持端口转发时才有效。

Client:客户:

import socket              

s = socket.socket()        
host = '103.47.59.130' 
port = 5001               

s.connect((host, port))
while True: 
    try:
        print "From Server: ", s.recv(1024) 
        s.send(raw_input("Client please type: "))
    except:
        break
s.close()

Server:服务器:

import socket               

s = socket.socket()         # Create a socket object
host = '192.168.0.104'    #private ip address of machine running fedora
port = 5006                
s.bind((host, port))       

s.listen(5)                
c, addr = s.accept()       
print 'Got connection from', addr    
while True:
   c.send(raw_input("Server please type: "))
   print "From Client: ", c.recv(1024)

c.close()

You need to use your public IP address or 0.0.0.0 in your server, and make sure your modem/router allows incoming connections at the specified port (usually called Port Forwarding).您需要在服务器中使用您的公共 IP 地址或0.0.0.0 ,并确保您的调制解调器/路由器允许指定端口的传入连接(通常称为端口转发)。

Hope it helps!希望能帮助到你!

Seems like you need to perform basic network troubleshooting.似乎您需要执行基本的网络故障排除。 Your description says you can connect to other machines on your own network, but not a machine on another network.您的描述说您可以连接到您自己网络上的其他机器,但不能连接到另一个网络上的机器。 You might try the same sorts of tests on the machine on the other network: can it connect to other machines on its own network, can it connect to other machines off of its network.您可以在另一个网络上的机器上尝试相同类型的测试:它是否可以连接到自己网络上的其他机器,是否可以连接到其网络之外的其他机器。

Some tools which are very valuable include ping, tracepath, tcpdump, and nc (netcat).一些非常有价值的工具包括 ping、tracepath、tcpdump 和 nc (netcat)。

Ultimately, if you can make the connection with netcat, you can assume the problem is with your code, but if you can't you can try and find the networking problem.最终,如果您可以与 netcat 建立连接,您可以假设问题出在您的代码上,但如果您不能,您可以尝试查找网络问题。

i know this isn't a part of your question but you wrote the print function wrong我知道这不是你的问题的一部分,但你写错了打印功能

how i see your code: print "Hello world"我如何看待您的代码:打印“Hello world”

how the code should be: print("Hello world")代码应该如何: print("Hello world")

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

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