使用请求模块正确分配 cookie
properly assign cookies with requests module
I have found the solution to my syntax error, however I do not understand why original code did not work.
我尝试了多个不同的模块,例如 urllib
、requests
,甚至 sockets
。但是我被指示使用 requests
因为它的简单性和准确性。我不想透露太多信息,因为这对 HTB 来说是一个挑战,但是我联系过的人也只是告诉我 "ya man your syntax looks fine" 并且没有帮助....呸。如果由于剧透我需要删除 post,我会立即删除。
下面的代码片段有效
import requests
import hashlib
s = requests.session()
url = 'someURL'
r = s.get(url)
cookie = s.cookies.get_dict() ##CHANGING THIS WORKED
x = r.text[167:187] #grabbing specific string
a = hashlib.md5(x.encode('utf-8')).hexdigest() ### CHANGING THIS WORKED
# b = s.post(url, data=a, cookies={'PHPSESSID': '{cookie}'})
final = s.post(url, data={'hash':a}, cookies=cookie)
print(final.text)
我希望 PHPSESSID 在请求期间以 cookie 形式传回服务器。上面的语法有效,但这不...
import requests
import hashlib
s = requests.session()
url = 'someURL'
r = s.get(url)
cookie = s.cookies['PHPSESSID']
x = r.text[167:187]
h = hashlib.md5()
h.update(x)
a = h.hexdigest()
b = s.post(url, data=a, cookies={'PHPSESSID': '{cookie}'})
print(b.text)
如果我想给自己分配一个 cookie 怎么办?我不明白 get_dict()
是如何工作的,而另一个不是。
The cookie is actually passed correctly in both situations. the key difference being the way the hashlib
module handles unicode-objects
.
解决方案
因为 x
是 unicode-object,我必须在发送到 hexdigest()
之前对其进行编码
这就是为什么
h = hashlib.md5(x.encode('utf-8')).hexdigest()
有效,另一个无效。简单地编码 x 就可以了。
I have found the solution to my syntax error, however I do not understand why original code did not work.
我尝试了多个不同的模块,例如 urllib
、requests
,甚至 sockets
。但是我被指示使用 requests
因为它的简单性和准确性。我不想透露太多信息,因为这对 HTB 来说是一个挑战,但是我联系过的人也只是告诉我 "ya man your syntax looks fine" 并且没有帮助....呸。如果由于剧透我需要删除 post,我会立即删除。
下面的代码片段有效
import requests
import hashlib
s = requests.session()
url = 'someURL'
r = s.get(url)
cookie = s.cookies.get_dict() ##CHANGING THIS WORKED
x = r.text[167:187] #grabbing specific string
a = hashlib.md5(x.encode('utf-8')).hexdigest() ### CHANGING THIS WORKED
# b = s.post(url, data=a, cookies={'PHPSESSID': '{cookie}'})
final = s.post(url, data={'hash':a}, cookies=cookie)
print(final.text)
我希望 PHPSESSID 在请求期间以 cookie 形式传回服务器。上面的语法有效,但这不...
import requests
import hashlib
s = requests.session()
url = 'someURL'
r = s.get(url)
cookie = s.cookies['PHPSESSID']
x = r.text[167:187]
h = hashlib.md5()
h.update(x)
a = h.hexdigest()
b = s.post(url, data=a, cookies={'PHPSESSID': '{cookie}'})
print(b.text)
如果我想给自己分配一个 cookie 怎么办?我不明白 get_dict()
是如何工作的,而另一个不是。
The cookie is actually passed correctly in both situations. the key difference being the way the
hashlib
module handlesunicode-objects
.
解决方案
因为 x
是 unicode-object,我必须在发送到 hexdigest()
之前对其进行编码
这就是为什么
h = hashlib.md5(x.encode('utf-8')).hexdigest()
有效,另一个无效。简单地编码 x 就可以了。