使用python socket

时间:2017-04-08 11:56:11

标签: python sockets network-programming

我想使用python套接字检索udp数据包的流量类(TOS)。下面的代码在python中创建套接字,我想检索类似于(java套接字中的getTrafficClass方法)的流量类。

UDP_IP = '127.0.0.1'
UDP_PORT = 8080
BUFFER_SIZE = 20

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.bind((UDP_IP, UDP_PORT))
data, address = s.recvfrom(BUFFER_SIZE)
if data:
  print "received data:", data
  //I need to get the traffic class here.

1 个答案:

答案 0 :(得分:1)

我认为java中没有类似getTrafficClass()的现成方法。你可以读取IP层头(UDP层头下面的一个)并解析它,示例代码在Sniffy.py https://github.com/OffensivePython/Sniffy/blob/master/Sniffy.pyhttps://codingsec.net/2016/05/decoding-ip-layer-python/

[...]
def sniff(sock):
""" sniff a packet, parse its header and dump the sniffed data """
packet, address = sock.recvfrom(65565)
ipheader=ip(packet[:20])
ipheader.parse()
[...]