How To Generate Tcp,ip And Udp Packets In Python?

好久不见. 提交于 2019-12-20 14:12:15

问题


Can anyone tell me what is the basic step to generate UDP, TCP and IP Packets. And how can i generate it using Python?


回答1:


as suggested by jokeysmurf you might craft packets with scapy

if you you want to send/receive usual packets then you should use socket or socketserver

  • http://docs.python.org/library/socket.html#module-socket
  • http://docs.python.org/library/socketserver.html#module-SocketServer

to send TCP to google's port 80 use

    import socket
    HOST = 'google.com'    # The remote host
    PORT = 80              # The same port as used by the server
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.send('GET / HTTP/1.1\r\nHost: google.com\r\n\r\n')
    data = s.recv(1024)
    s.close()
    print 'Received', repr(data)

to make it udp change SOCK_STREAM to SOCK_DGRAM




回答2:


You can do interactive packet manipulation with scapy.

This article is going to get you started on gluing together an IP packet.

Construction of a tcp packet is as easy as:

packet = IP(src="10.0.0.10")



来源:https://stackoverflow.com/questions/8196886/how-to-generate-tcp-ip-and-udp-packets-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!