Python 这两行在代码中是什么意思?
“:”和“+2”在Python 这两行在代码中是什么意思?,python,python-3.x,python-2.7,penetration-testing,Python,Python 3.x,Python 2.7,Penetration Testing,“:”和“+2”在[:http\u payload.index(“\r\n\r\n”)+2]中是什么意思? “(“/”)和“[1]”在.split(“/”[1]中是什么意思 def get_http_headers(http_payload): try: # split the headers off if it is HTTP traffic headers_raw = http_payload[:http_payload.index("\r\n\r\n
[:http\u payload.index(“\r\n\r\n”)+2]
中是什么意思?
“(“/”)和“[1]”在.split(“/”[1]
中是什么意思
def get_http_headers(http_payload):
try:
# split the headers off if it is HTTP traffic
headers_raw = http_payload[:http_payload.index("\r\n\r\n")+2]
# break out the headers
headers = dict(re.findall(r"(?P<name>.*?): (? P<value>.*?)\r\n", headers_raw))
except:
return None
return headers
def extract_image(headers, http_payload):
image = None
image_type = None
try:
if "image" in headers["Content-Type"]:
# grab the image type and image body
image_type = headers["Content-Type"].split("/")[1]
image = http_payload[http_payload.index("\r\n\r\n")+4:]
except:
pass
except:
return None, None
return image, image_type
def get_http_头(http_有效负载):
尝试:
#如果是HTTP流量,则将头拆分
headers\u raw=http\u负载[:http\u负载.index(“\r\n\r\n”)+2]
#打开标题
headers=dict(关于findall(r“(?P.*?):(?P.*?)\r\n”,headers\u raw))
除:
一无所获
返回标题
def extract_映像(标题、http_有效负载):
图像=无
图像类型=无
尝试:
如果标题中的“图像”[“内容类型”]:
#抓取图像类型和图像主体
图像类型=标题[“内容类型”]。拆分(“/”[1]
image=http\u负载[http\u负载.索引(“\r\n\r\n”)+4:]
除:
通过
除:
返回None,None
返回图像,图像类型
http\u有效负载[:http\u payload.index(“\r\n\r\n”)+2]
字符串http\u有效负载
,因此只有第一次出现“\r\n\r\n”之前的字符串头和第一个“\r\n”仍然保留。字符串的.index()
方法将返回该模式在字符串中第一次出现的索引
例如:
test = "abcdefg"
# slicing:
print(test[1:3]) # will output 'bc'
# index:
print(test.index('bc')) # will output 1 (index of start of substring 'bc')
# either start or end (or both) of the slice can be left out, so the following is equivalent:
print(test[:2] == test[0:2]) # will output True
.split(“/”[1]
将在“/”字符处拆分字符串,并返回一个列表,从中可以访问索引为1的项。
例如,请参见以下代码:
test = "/this/is/a/path"
print(test.split("/")) # will output ["this", "is", "a", "path"]
print(test.split("/")[0]) # will output "is" since element of index 1 of the resulting list is accessed.
这里有3个不同的问题(或5个,取决于你如何计算)。这不是这里所期望的格式,这是您遇到的一个特定问题,有一些证据表明您试图解决它。@SamuelDion Girardeau问题已更新。如果您能回答,我们将不胜感激!非常感谢。和的副本。请在此页中查找切片。或者非常感谢!你能给每个人举个简单的例子吗?“[:http_payload.index(“\r\n\r\n”)+2]”中“:”和“+2”的含义是什么?非常感谢你的帮助!非常感谢您的帮助@请阅读提供的链接。“:”分隔切片的开始索引和结束索引
http\u payload.index(“\r\n\r\n”)
将返回一个整数,其中添加“+2”。因此,如果计算结果为10(例如),它将相当于http\u有效载荷[:12]
,它是http\u有效载荷[0:12]
的缩写。这基本上意味着“从stringhttp_有效负载中获取字符0到12”。在询问有关StackOverflow的问题之前,请阅读您选择的Python教程并参考Python文档。StackOverflow既不是前者的替代品,也不是后者的替代品。