在 python 中创建 IPv4 数据包标头

发布于 2024-10-27 16:08:39 字数 454 浏览 5 评论 0原文

对于我的较大项目,我需要从创建 IPv4 网络数据包(用于模拟)开始,

同样,我需要编写一个函数,该函数从传递的数据中创建一个标头,其中包含源 ip、目标 ip 和所有必要的

def convertIpv4( data ):
     pass

字段我需要你引导我走向正确的方向。

首先,我需要以 32 位存储 IP,因此,如果我有一个字符串“192.168.2.1”,将其转换为字节的最有效方法是什么?大小也是 32 ?

另外,如果我首先创建一个带有诸如 version = 4 和 sourceip 和 dest ip 之类的内容的普通类,那么有没有办法将其直接转换为字节数组,其中对象的位置就像以下标头 IPHeader

请告诉我应该如何进行......

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 IPHeader

Please tell how should i proceed....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-11-03 16:08:39

Scapy 可以轻松创建 IP 标头。
这里他们有一个创建 IP 标头的示例。

Scapy can create IP headers easily.
Here they have an example on creating IP header.

感性不性感 2024-11-03 16:08:39

查看 PyIP 模块。

Have a look at the PyIP module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文