首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python:不在LAN上工作的套接字

Python:不在LAN上工作的套接字
EN

Stack Overflow用户
提问于 2013-03-25 05:23:44
回答 2查看 7.8K关注 0票数 0

我最近度假回来了,我的基本python 2套接字服务器现在无法通过LAN与客户端通信。服务器在mac上,客户端是我的raspberry pi或windows 7机器。我在这里简化了服务器和客户端代码,给出了一个示例:

服务器

代码语言:javascript
运行
复制
import socket
from thread import *

HOST = socket.gethostname()

print HOST

PORT = input ("Enter the PORT number (1 - 10,000)")



s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print "Socket Created"


s.bind((HOST, PORT))

print "Socket Bind Complete"

s.listen(10)
print "Socket now listening"


    #Sending message to connected client
    #This only takes strings (words


while True:
    #Wait to accept a connection - blocking call
    connection, addr = s.accept()
    print "Connection Established!"

    connection.send("Welcome to the server. Type something and hit enter\n")

    #loop so that function does not terminate and the thread does not end
    while True:

        #Receiving from client
        data = connection.recv(1024)
        if not data:
            break
        connection.sendall(data)
        print data
    connection.close()
s.close()

客户端

代码语言:javascript
运行
复制
import socket #for sockets
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket Created"

#Get host and port info to connect
host = raw_input("HOST >>>   ")
port = 2468
s.connect((host, port))


while True:   
    #Send some data to the remote server
    message = raw_input(">>>  ")

    #set the whole string
    s.sendall(message)


    reply = s.recv(1024)
    print reply

问题

这里发生什么事情?我正在获得本地IP,但脚本仍然无法通信。这会不会是操作系统的问题?

更多信息

  1. 乒乓 我在我的Mac终端上打开了PI: (67.63.55.3):56个数据字节67.63.55.3: icmp_seq=0 ttl=240 time=17.434 ms 64字节67.63.55.3: icmp_seq=1 ttl=240 time=18.180 ms 64字节67.63.55.3: icmp_seq=2 ttl=240 time=22.046 ms 64字节从67.63.55.3: icmp_seq=3 ttl=240 time=25.124 64字节从67.63.55.3: icmp_seq=4 ttl=240 time=31.773 ms 我的PI没能找到Mac作为主机。我会看看我能做些什么来补救这个问题。 我的电脑能够打开我的mac电脑。我的Mac能够打开我的电脑
  2. 防火墙

我的Mac防火墙关闭了。我将检查Raspberry Pi Stackexchange站点,看看PI是否有防火墙。

一旦我测试了我的windows机器,我会添加更多的信息。

EN

回答 2

Stack Overflow用户

发布于 2013-03-25 08:27:40

在本地运行这两个脚本,它们可以在我的机器上连接和通信。您正面临一个网络问题,这个问题应该很容易调试。

  1. 错误绑定。在服务器上,打印你得到的主机。如果服务器有多个IP,您可能会尝试绑定错误的IP。您还可以将其更改为“0.0.0.0”(仅在服务器端),并查看是否有效。
  2. 防火墙。任何一方都可能在操作系统级别上阻塞tcp通信。调试通过Windows上的Wireshark和unix上的tcpdump完成。开始嗅探,运行您的代码,看看出了什么问题。您很可能会看到客户端发送SYN数据包,但服务器将无法使用SYN|ACK数据包进行应答。如果您看到SYN数据包到达服务器,请尝试完全关闭服务器的防火墙,然后再试一次。否则,客户端将被禁止对外通信(不太可能),您将需要关闭它的防火墙。
  3. In use port.尝试删除SO_REUSEADDR进行调试,并查看是否有什么变化。
  4. 异常。确保您不会忽略套接字中的任何异常。
票数 0
EN

Stack Overflow用户

发布于 2013-09-23 11:42:24

您的代码运行良好,不过,我做了一些小更正,并在代码中的注释中对它们进行了解释:

服务器:

代码语言:javascript
运行
复制
import socket
from thread import *

# 1.Gets the local ip/ip over LAN.
HOST =socket.gethostbyname(socket.gethostname()) 

print HOST

# 2.Use port no. above 1800 so it does not interfere with ports already in use.
PORT =input ("Enter the PORT number (1 - 10,000)") 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print "Socket Created"
s.bind((HOST, PORT))
print "Socket Bind Complete"
s.listen(10)
print "Socket now listening"
while True:
    connection, addr = s.accept()
    print "Connection Established!"
    connection.send("Welcome to the server. Type something and hit enter\n")
    while True:
        data = connection.recv(1024)
        if not data:
            break
        connection.sendall(data)
        print data
        connection.close()
s.close()

客户端:

代码语言:javascript
运行
复制
import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket Created"
host = raw_input("HOST >>>   ")

# 3. Use the same port no. entered on server.py as the client binds to the 
#    same port
port = input ("Enter the PORT number (1 - 10,000)") 
s.connect((host, port))

while True:   
    message = raw_input(">>>  ")    
    s.sendall(message)
    reply = s.recv(1024)
    print reply

上面的代码对我来说很好,我相信它对你也是一样的,因为我也经历过同样的麻烦。发现的bugs -我把它们放在代码中的注释中--请看一看。

干杯……!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15608189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档