from_buffer_copy()的语法
我正在读一本名为 Black Hat的书 Python Python编程针对黑客和五旬节的编程 贾斯汀·塞兹(Justin Seitz)。
这是本书中的代码:
import socket
import os
import struct
from ctypes import *
# host to listen
host = "10.0.2.15"
# our IP header
class IP(Structure):
_fields_ = [
("ihl", c_ubyte, 4),
("version", c_ubyte,4),
("tos", c_ubyte),
("len", c_ushort),
("id", c_ushort),
("offset", c_ubyte),
("ttl", c_ubyte),
("protocol_num", c_ubyte),
("sum", c_ubyte),
("src", c_ulong),
("dst", c_ulong)
]
def __new__(cls,socket_buffer=None):
return cls.from_buffer_copy(socket_buffer)
在此代码中,在函数新的内部,有名为from_buffer_copy()
的函数。它是ctypes
python库的功能。
我想知道它的作用以及使用它的语法是什么?
Python文档说该语法是from_buffer_copy(source [,offset])
。我发现了使用此语法的示例,但是文档提供的用法定义对我来说是模糊的。
Python文档 仅用一个参数看到了此功能,那么它如何实现?
任何帮助将不胜感激。
I'm reading a book called Black Hat
Python Python Programming for Hackers and Pentesters
by Justin Seitz.
This is code from the book:
import socket
import os
import struct
from ctypes import *
# host to listen
host = "10.0.2.15"
# our IP header
class IP(Structure):
_fields_ = [
("ihl", c_ubyte, 4),
("version", c_ubyte,4),
("tos", c_ubyte),
("len", c_ushort),
("id", c_ushort),
("offset", c_ubyte),
("ttl", c_ubyte),
("protocol_num", c_ubyte),
("sum", c_ubyte),
("src", c_ulong),
("dst", c_ulong)
]
def __new__(cls,socket_buffer=None):
return cls.from_buffer_copy(socket_buffer)
In this code, inside the function new, there is function called from_buffer_copy()
. It is a function from the ctypes
Python library.
I want to know what it does and what is the syntax to use it?
The Python documentation says the syntax is from_buffer_copy(source[, offset])
. I have found examples that use this syntax, but the usage definition provided by the documentation is blurry to me.
Python documentation: from_buffer_copy()
In this book I saw this function with only one argument, so how is it being implemented?
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在文档
from_buffer_copy(源[,offset])
中,方括号表示第二个参数是可选的。在代码中,它如下所示,其中给出了可选第二个参数的默认值:这是
.from_buffer_copy 和
.from_buffer
带有说明的工作示例。 。这些功能用于创建基于接收到字节数据的内存结构的python类:输出:
In the documentation
from_buffer_copy(source[, offset])
, square brackets means the 2nd parameter is optional. In code it is written like the following, where a default value for the optional second parameter is given:Here's a working example of the difference between
.from_buffer_copy
and.from_buffer
with explanations. The functions are used to create Python classes based on memory structures that are received as byte data:Output: