在 python 中创建 IPv4 数据包标头
对于我的较大项目,我需要从创建 IPv4 网络数据包(用于模拟)开始,
同样,我需要编写一个函数,该函数从传递的数据中创建一个标头,其中包含源 ip、目标 ip 和所有必要的
def convertIpv4( data ):
pass
字段我需要你引导我走向正确的方向。
首先,我需要以 32 位存储 IP,因此,如果我有一个字符串“192.168.2.1”,将其转换为字节的最有效方法是什么?大小也是 32 ?
另外,如果我首先创建一个带有诸如 version = 4 和 sourceip 和 dest ip 之类的内容的普通类,那么有没有办法将其直接转换为字节数组,其中对象的位置就像以下标头
请告诉我应该如何进行......
For my larger project i need to start with creating a IPv4 network packet ( for simulation )
for the same I need to write a function that creates a header out of the passed data which has the source ip , dest ip and all the necessary fields
def convertIpv4( data ):
pass
For this I need you to guide me in the right direction.
Firstly I need to store the IP in 32 bits so for that if I have a string "192.168.2.1" what is the most efficient way to convert it into bytes and that too a size of 32 ??
Also if I create first a normal class with stuff like version = 4 and sourceip and dest ip then is there a way to convert it directly into a byte array with the position of objects just like the following header
Please tell how should i proceed....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Scapy 可以轻松创建 IP 标头。
这里他们有一个创建 IP 标头的示例。
Scapy can create IP headers easily.
Here they have an example on creating IP header.
查看 PyIP 模块。
Have a look at the PyIP module.