【发布时间】:2015-08-07 22:09:14
【问题描述】:
尝试通过获取请求在我的电脑上发送带有特定 cookie 的 Post 请求
我在谷歌搜索然后我找到了
opener = urllib2.build_opener() # send the cookies
opener.addheaders.append(('Cookie', cookies)) # send the cookies
f = opener.open("http://example")
这段代码很有用,对我有帮助
但有人可以为我解释一下并告诉我 f 变量 是否提出请求?
我不需要 cookielib 只是我的例子 :)
如果我输入了
url = 'http://example' # 要知道值输入任何密码以知道 cookie
values = {"username" : "admin",
"passwd" : "1",
"lang" : "" ,
"option" : "com_login",
"task" : "login",
"return" : "aW5kZXgucGhw",
} # request with the hash
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
cookies=response.headers['set-cookie'] #to get the cookies
opener = urllib2.build_opener() # send the cookies
opener.addheaders.append(('Cookie', cookies)) # send the cookies
f = opener.open("http://example.com)
两个post请求会发生什么!?
【问题讨论】:
-
阅读 urllib2 的文档。它将回答您关于图书馆如何运作的问题。如果您对如何实现逻辑有具体问题,请发布问题。 docs.python.org/2/library/urllib2.html