【问题标题】:send cookies inside post request在 post 请求中发送 cookie
【发布时间】: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请求会发生什么!?

【问题讨论】:

标签: python post cookies


【解决方案1】:

当您在 opener 对象上调用 open() 方法时,请求将被发送。 f 变量包含对打开连接的引用,以防您稍后想要对其执行其他操作(例如再次关闭它)。

您说“发送 cookie”的 cmets 放错地方了,您拨打 append 的那一行只是准备请求,只有在您拨打 open 时才会发送。

【讨论】:

  • 我不知道你在问什么
  • 明确一点 :) 首先,如果我添加了 opener = urllib2.build_opener() # 发送 cookie opener.addheaders.append(( 'Cookie', cookies)) # 发送 cookie f = opener.open("example.com") 变量 f 会发出另一个请求还是相同的请求?:)
  • 在我的示例中,当我运行脚本时 f 变量会发出请求??
猜你喜欢
  • 2015-10-23
  • 2016-06-14
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 2013-01-05
  • 2018-07-20
  • 2011-04-12
  • 2022-06-12
相关资源
最近更新 更多