【问题标题】:trying to bind to specific external ip:: [Errno 10049] The requested address is not valid in its context could not open socket-(python 2.7)试图绑定到特定的外部 ip:: [Errno 10049] 请求的地址在其上下文中无效,无法打开套接字-(python 2.7)
【发布时间】: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


【解决方案1】:

socket.INADDR_ANY 等于 socket.bind('0.0.0.0')

如果绑定到“0.0.0.0”可以监听所有接口(可用)

Moxa TCP 示例:

import socket,time
import thread

#Example client
class _client :
    def __init__(self):
        self.status = False
    def run(self,clientsock,addr):
        while 1 :
            try:
                data = clientsock.recv(BUFF)
                if data :
                    #do something with data
                    time.sleep(.1)
                    if self.status == False:
                        clientsock.close()
                        break
                    clientsock.send(next_query)


             except Exception,e :
                print e
                break
client = _client()
ADDR = ("0.0.0.0", 45500) #so you need port how to decode raw socket data ?
serversock = socket(AF_INET, SOCK_STREAM)
serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)#gain 50ms tcp delay
serversock.bind(ADDR)
serversock.listen(10)

While True :
    clientsock, addr = serversock.accept()
    if addr[0] == device_1_IP:
        client.status = True
        #start device1 thread(moxa is TCP client mode)
        thread.start_new_thread(client.run, (clientsock, addr))
        #if client.status = False you will be close connection.

但我的提议是“在 TCP 服务器模式下使用 moxa”

我在 120 台设备上使用 4x5450i 3x5250 没有任何错误。

【讨论】:

  • dsgdfg 所以我 sock 与客户端连接并保持连接并等待接收?{while 1: received = sock.recv(10000)}
  • dsgdfg 所以,您创建服务器,然后将 hem 与尝试从 moxa tcp 消息接收的客户端连接(您不使用您传入 _client::run 的 addr 参数)?..谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 2011-06-07
  • 2014-12-31
  • 1970-01-01
  • 2020-08-05
  • 2016-08-26
  • 1970-01-01
相关资源
最近更新 更多