Python中通过互联网的套接字连接? [英] Socket connection over internet in Python?

查看:73
本文介绍了Python中通过互联网的套接字连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 2.7.x 中创建了一个基本的客户端服务器套接字程序,即使在不同的机器上,它也可以在同一网络上运行得非常好,但是当我在不同的网络上运行服务器和客户端时(服务器在我朋友的网络上,而客户端在我的)它不会返回任何错误并继续等待.我只是不明白如何调试代码.我通过杀死端口 80 上的所有服务来使用端口 80.我还在两台机器上的端口 80 上进行了端口转发.

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. 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.

我的代码如下:

client.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

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

有时输出 **From Server: ** 但不会来回发送任何消息.

It sometimes output **From Server: ** but doesnot send any message back and forth.

PS:我之前在 Stack Overflow 上搜索过,但找不到任何相关内容.

PS: I have searched on Stack Overflow earlier but I am unable to find anything relevant.

推荐答案

使用这个软件来实现转发端口.我建议您为您的服务器使用另一个端口,比如 5006,以防止与使用非常常用的端口(如 80)相关的任何问题.基本上,该软件的工作原理如下:

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:

  • 您单击连接",它会搜索路由器,如果找到您的路由器,则会列出现有的端口映射.
  • 您创建端口映射(在右侧),默认协议为 TCP
  • 您在路由器上选择一个端口,例如 5001(称为外部端口)
  • 您在服务器上选择一个端口,可能是 5006(称为内部端口)
  • 然后会指示路由器使用您的私有 IP 将所有到达端口 5001 的数据转发到您的服务器,特别是转发到您服务器上的端口 5006.

因此,您的客户端所要做的就是将数据发送到您服务器的公共 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.

客户:

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

服务器:

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

这篇关于Python中通过互联网的套接字连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆