我最近从休假回来,现在我的基本python 2套接字服务器无法通过局域网与客户端进行通信。服务器在Mac上,客户端是我的树莓派或我的Windows 7机器。我在这里简化了服务器和客户端的代码来举个例子:Python:无法通过局域网连接的套接字
服务器
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,但脚本仍无法通信。这可能是操作系统的问题吗?
MORE INFO
执行ping
一个。我能够从我的Mac终端ping P1:
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 ms
b。我的PI无法找到Mac作为主机。我会看看我能做些什么来弥补这一点。
c。我的电脑能够PING我的Mac。我的Mac能够ping通我的电脑
防火墙
我的Mac的防火墙关闭。我将检查[Raspberry Pi Stackexchange Site]以查看PI是否有防火墙。
我会添加更多的信息,一旦我测试我的Windows机器
你可以ping其他机器吗? SSH他们?他们的IP地址是静态的还是动态的? – jozzas 2013-03-25 05:53:40
@jozzas不知道IP的(我知道我的外部是动态的)Pinging信息被添加。 – xxmbabanexx 2013-03-27 19:18:03
在Linux下,如果zou不是超级用户,禁止绑定到低于1024的端口。 – User 2014-01-16 16:55:04