简体   繁体   中英

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 . 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 .

#!/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 .

Here is the output of tcpdump for my rtp stream

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

Here is the output of tcpdump from netcat

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 ............

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 .

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.

Thanks for the comments and taking the time to look .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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