我最近從休假回來,現在我的基本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