简体   繁体   English

python3 socket recvfrom仅从netcat接收数据

[英]python3 socket recvfrom only receives data from netcat

I am attempting to create a simple python program for rtp sequence tracking but have run into an odd problem . 我试图为rtp序列跟踪创建一个简单的python程序,但是遇到了一个奇怪的问题。 I can see the rtp data on a tcpdump but when I run my script recvfrom just sits there , I confirmed the port is open with netstat , and if I send data using netcat the script does receive data . 我可以在tcpdump上看到rtp数据,但是当我运行脚本recvfrom时,我确认端口已通过netstat打开,并且如果我使用netcat发送数据,则脚本确实会接收数据。

#!/usr/bin/python3

import socket, threading, time
import datetime, sys



def main():
    """
    MAIN
    """
    #udp = rxUdp()
    #udp.startUDPRx()

    udp_ip = '192.168.1.100'
    udp_port = 6022

    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #IP/UDP
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    sock.bind(('', udp_port))
    #sock.bind(('eth1', 0))


    expected_rtp_seq = None
    print('Running')
    ts_file = open('test.ts', 'wb')
    #while True:
    for i in range (1, 1000):

        data = b''
        data, addr = sock.recvfrom(1500) #buffer size
        rtp_sequence_no = data[2:4]
        rtp_sequence_no = int.from_bytes(rtp_sequence_no, byteorder='big')

        rtp_len = data[19:20]

        mpegts_data = data[12:len(data)]
        print(rtp_sequence_no)
        print('.', end='')
        sys.stdout.flush()
        ts_file.write(mpegts_data)
        print(len(data))      
        if expected_rtp_seq == None:
            expected_rtp_seq = rtp_sequence_no
        if expected_rtp_seq != rtp_sequence_no:
            print('\n%s: Sequence Mismatch.  Expected %d, got %d' %
              (datetime.datetime.now(), expected_rtp_seq, rtp_sequence_no))
            expected_rtp_seq = rtp_sequence_no

        if expected_rtp_seq == 65535:
            expected_rtp_seq = 0
        else:
            expected_rtp_seq += 1


    ts_file.close()



if __name__ == '__main__':
    main()

I should mention this code does seem to work on windows 7 but not on ubuntu . 我应该提一下,这段代码似乎确实可以在Windows 7上运行,但不能在ubuntu上运行。

Here is the output of tcpdump for my rtp stream 这是我的rtp流的tcpdump的输出

10:26:57.486256 IP 209.87.232.169.57346 > 192.168.1.100.6022: UDP, length 1328 0x0000: 4500 054c 66e6 0000 3d11 95ad d157 e8a9 E..Lf...=....W.. 0x0010: c0a8 0164 e002 1786 0538 9797 8021 eddc ...d.....8...!.. 0x0020: 6512 e48b e7a0 747d 4700 6513 e.....t}Ge 10:26:57.486256 IP 209.87.232.169.57346> 192.168.1.100.6022:UDP,长度1328 0x0000:4500 054c 66e6 0000 3d11 95ad d157 e8a9 E..Lf ... = .... W .. 0x0010:c0a8 0164 e002 1786 0538 9797 8021 eddc ... d ..... 8 ...!.. 0x0020:6512 e48b e7a0 747d 4700 6513 e ..... t} Ge

Here is the output of tcpdump from netcat 这是netcat的tcpdump的输出

10:26:51.709234 IP 192.168.2.149.52305 > 192.168.2.241.6022: UDP, length 3 0x0000: 4500 001f 7d3c 4000 4011 36bb c0a8 0295 E...}<@.@.6..... 0x0010: c0a8 02f1 cc51 1786 000b 22c0 6869 0a00 .....Q....".hi.. 0x0020: 0000 0000 0000 0000 0000 0000 ............ 10:26:51.709234 IP 192.168.2.149.52305> 192.168.2.241.6022:UDP,长度3 0x0000:4500 001f 7d3c 4000 4011 36bb c0a8 0295 E ...} <@。@。6 ..... 0x0010: c0a8 02f1 cc51 1786 000b 22c0 6869 0a00 ..... Q ....“。hi .. 0x0020:0000 0000 0000 0000 0000 0000 ............

Thanks for looking 谢谢看

Okay guys figured it out will leave my solution here if anyone else is banging their head against a wall. 好的,如果其他人将头撞在墙上,他们想出了解决方案。

So the issue was that net.ipv4.conf.all.rp_filter, net.ipv4.conf.eth1.rp_filter, net.ipv4.conf.eth2.rp_filter were all enabled found my answer here https://serverfault.com/a/216568 . 所以问题是net.ipv4.conf.all.rp_filter,net.ipv4.conf.eth1.rp_filter,net.ipv4.conf.eth2.rp_filter都已启用,在这里找到了我的答案https://serverfault.com/a / 216568

Basically what these configs do is filter out any data that is not sourced from your local lan to prevent spoofing I set them all to 0 and the script fired up. 基本上,这些配置的作用是过滤掉并非来自您本地局域网的任何数据,以防止欺骗,我将它们全部设置为0并启动了脚本。

Thanks for the comments and taking the time to look . 感谢您的评论并抽出宝贵的时间查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM