【发布时间】:2015-08-31 07:18:16
【问题描述】:
我有一个Moxa device 从串行数据创建 Tcp-ip 消息并通过 LAN 将它们发送给我。 我需要用 python 服务器收听他特定的外部 IP(172.16.0.77)。 生病尝试这样做:
BUFFER_SIZE = 10000 # Normally 1024, but we want fast response
HOST = self.TCP_IP #(172.16.0.77)
PORT = self.TCP_PORT # Arbitrary non-privileged port
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
print msg
s = None
continue
try:
s.bind(sa)
s.listen(1)
except socket.error as msg:
print msg
s.close()
s = None
continue
break
if s is None:
print 'could not open socket'
while s:
print s.getsockname()
conn, addr = s.accept()
print 'Connection address:', addr
data = conn.recv(BUFFER_SIZE)
if data:
self.emit(QtCore.SIGNAL("SamplesRecive"),data)
conn.close()
我得到:[Errno 10049] 请求的地址在其上下文中无效,无法打开套接字
我需要将服务分配给许多 Moxa 设备,所以我不能使用 socket.INADDR_ANY
有什么想法吗?
【问题讨论】:
-
您在哪一行收到此错误?
-
pss on [s.bind(sa)],谢谢
-
下面
dsgdfg提供的答案应该可以解决这个问题。否则,这里有一个相同的帖子,可能会帮助您找到原因。 stackoverflow.com/a/4657548/2382792
标签: python windows sockets networking tcp-ip