socket.recv(256)怎么能返回None?

时间:2013-01-20 01:22:03

标签: python sockets

有2个python程序,host1.pyhost2.py同时运行并通过socket进行通信,以便 -

host1.pyreciveSock = socket(AF_INET,SOCK_DGRAM)

host2.pysendSocket = socket(AF_INET,SOCK_DGRAM)

套接字都是相互绑定的。

host1.py有 -

try:
    msgBacked = reciveSock.recv(256)
except:
    pass
if( msgBacked is None):
    print "isNone!"

确实打印isNone!表示msgBackedNone

host2.pyhost1.py的所有发送都属于sendSocket.send("ACK")类型,因此发送的字符串中有一个值传输到host1.py

那么msgBacked如何成为None

1 个答案:

答案 0 :(得分:3)

最有可能receiveSock.recvthrowing a timeout exception,而msgBacked之前已设为None。尝试删除try .. except,看看会发生什么。