【发布时间】:2013-03-14 13:02:52
【问题描述】:
我最近从假期回来,我的基本 python 2 套接字服务器现在无法通过 LAN 与客户端通信。服务器在 Mac 上,客户端是我的树莓派或我的 windows 7 机器。我这里简化了服务端和客户端的代码举个例子:
服务器
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()
客户
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,但脚本仍然无法通信。会不会是操作系统的问题?
更多信息
-
ping
一个。我能够从我的 Mac 终端 ping PI:
PING raspberrypi (67.63.55.3): 56 data bytes 64 bytes from 67.63.55.3: icmp_seq=0 ttl=240 time=17.434 ms 64 bytes from 67.63.55.3: icmp_seq=1 ttl=240 time=18.180 ms 64 bytes from 67.63.55.3: icmp_seq=2 ttl=240 time=22.046 ms 64 bytes from 67.63.55.3: icmp_seq=3 ttl=240 time=25.124 ms 64 bytes from 67.63.55.3: icmp_seq=4 ttl=240 time=31.773 msb.我的 PI 无法找到作为主机的 Mac。我会看看我能做些什么来解决这个问题。
c。我的电脑能够 PING 我的 mac。我的 Mac 能够 ping 我的电脑
防火墙
我的 Mac 的防火墙已关闭。我将在 [Raspberry Pi Stackexchange Site] 上查看 PI 是否有防火墙。
我会在测试我的 windows 机器后添加更多信息
【问题讨论】:
-
你能ping通其他机器吗? SSH 他们?他们的 IP 地址是静态的还是动态的?
-
@jozzas 不确定 IP(我知道我的外部是动态的) Ping 信息已添加。
-
Linux下zuo maz如果不是超级用户,则禁止绑定低于1024的端口。
标签: python macos sockets python-2.7 raspberry-pi