Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 丢弃数据报套接字的传入“数据包”_Python_Sockets_Udp_Datagram - Fatal编程技术网

Python 丢弃数据报套接字的传入“数据包”

Python 丢弃数据报套接字的传入“数据包”,python,sockets,udp,datagram,Python,Sockets,Udp,Datagram,这是一个真正集中在我的问题上的问题,而不是我在这个话题上能找到的任何其他问题 PSA:当我说packet时,我指的是在单个套接字中接收到的完整字符串。recvmaxsize 我在我的pref语言上为相同的结果开发了类似的代码,现在我必须用python来做 我有两个并行运行的进程: 1-连接到特定IP的普通客户端套接字 2-绑定到所有IP的客户端数据报套接字 正常的套接字正常工作,而数据报没有正常工作 我以每秒5次以上的速率不断地从非我的、非开源的服务器接收数据包,但我希望每3秒只处理其中一个数据

这是一个真正集中在我的问题上的问题,而不是我在这个话题上能找到的任何其他问题

PSA:当我说packet时,我指的是在单个套接字中接收到的完整字符串。recvmaxsize

我在我的pref语言上为相同的结果开发了类似的代码,现在我必须用python来做

我有两个并行运行的进程: 1-连接到特定IP的普通客户端套接字 2-绑定到所有IP的客户端数据报套接字

正常的套接字正常工作,而数据报没有正常工作

我以每秒5次以上的速率不断地从非我的、非开源的服务器接收数据包,但我希望每3秒只处理其中一个数据包。在java中,我只进行了一次睡眠,结果还行,我只得到了最后一个实时数据包,而在Python中,我得到了一个time.sleep3数据包是排队的,我不知道如何和在哪里,也不知道如何丢弃数据包

我必须删除它们,因为它们不是必需的,我必须在一个和另一个之间进行HTTP调用,所以我不能对以这种速率接收到的每一组数据启动HTTP post

这是我的监听套接字代码,一些注释是针对私有代码的:

def listenPositions():
    lsSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    lsSocket.bind(("0.0.0.0", 8787))
    lsSocket.setblocking(0)
    try:
        while True:
            ready = select.select([lsSocket], [], [], 1)
            if ready[0]:
                lsSocket.settimeout(1)
                recvData = lsSocket.recv(16384)
                if len(recvData) != 0:
                    recv = recvData[0:len(recvData)].decode("utf-8")
                    #print("LS: Received: " + recv)
                    strings = filter(None, str(recv).split('\n'))
                    print("Strings count=" + str(len(strings))+ ": " + str(strings))
                    for item in strings:
                       #parse the received strings as json and get the items
                        jsonPosition = json.loads(item)
                        strId = jsonPosition["id"]
                        coordinates = jsonPosition.get("coordinates")
                        if coordinates is None:
                            continue
                        print("coordinates not null:" + str(coordinates))
#DO THE HTTP POST REQUEST


                    time.sleep(3) #Pause the system for X seconds, but other packets are queued!
                else:
                    print("LS: Received empty")
            else:
                print("LS: No data, timeout")
    except Exception as e:
        print(e)
        #handle exceptions...
        print("Exception, close everything")

当您有一个打开的套接字时,所有正确寻址的数据包都应该发送到应用程序。我们希望我们的网络连接尽可能可靠,不是吗?丢包是最后的手段

如果您只想不时地获取数据包,可以创建一个侦听套接字,获取数据包并关闭套接字

然而,没有什么比忽略数据包更容易的了。跳过它的处理,继续前进。下面的代码不完整,但希望能表达我的意思

TIMEOUT = 1.0
INT = 3.0        # interval in seconds

# create udp_socket
last = time.time() - INT
udp_socket.settimeout(TIMEOUT)
while True:
    try:
        packet = udp_socket.recv(MAXSIZE)
    except socket.timeout:
        # handle recv timeout
        continue    # or break, or return
    except OSError:
        # handle recv error (Python 3.3+)
        break       # or continue, or return
    now = time.time()
    if now - last >= INT:
        # process the packet
        last = now
请注意,如果仅从一个源读取,则不需要选择