使用 python urllib2 在http标头中传递会话cookie?

发布于 2024-12-01 00:47:47 字数 1561 浏览 5 评论 0原文

我正在尝试编写一个简单的脚本来登录维基百科并使用 Mediawiki api 在我的用户页面上执行一些操作。但是,我似乎从未通过第一个登录请求(从此页面: https:/ /en.wikipedia.org/wiki/Wikipedia:Creating_a_bot#Logging_in)。我认为我设置的会话 cookie 没有被发送。到目前为止,这是我的代码:

import Cookie, urllib, urllib2, xml.etree.ElementTree

url = 'https://en.wikipedia.org/w/api.php?action=login&format=xml'
username = 'user'
password = 'password'

user_data = [('lgname', username), ('lgpassword', password)]

#Login step 1
#Make the POST request
request = urllib2.Request(url)
data = urllib.urlencode(user_data)
login_raw_data1 = urllib2.urlopen(request, data).read()

#Parse the XML for the login information
login_data1 = xml.etree.ElementTree.fromstring(login_raw_data1)
login_tag = login_data1.find('login')
token = login_tag.attrib['token']
cookieprefix = login_tag.attrib['cookieprefix']
sessionid = login_tag.attrib['sessionid']

#Set the cookies
cookie = Cookie.SimpleCookie()
cookie[cookieprefix + '_session'] = sessionid

#Login step 2
request = urllib2.Request(url)
session_cookie_header = cookieprefix+'_session='+sessionid+'; path=/; domain=.wikipedia.org; HttpOnly'

request.add_header('Set-Cookie', session_cookie_header)
user_data.append(('lgtoken', token))
data = urllib.urlencode(user_data)

login_raw_data2 = urllib2.urlopen(request, data).read()

我认为问题出在 request.add_header('Set-Cookie', session_cookie_header) 行中的某个位置,但我不确定。如何使用这些 python 库在每个请求的标头中发送 cookie(这对于许多 API 函数来说是必需的)。

I'm trying to write a simple script to log into Wikipedia and perform some actions on my user page, using the Mediawiki api. However, I never seem to get past the first login request (from this page: https://en.wikipedia.org/wiki/Wikipedia:Creating_a_bot#Logging_in). I don't think the session cookie that I set is being sent. This is my code so far:

import Cookie, urllib, urllib2, xml.etree.ElementTree

url = 'https://en.wikipedia.org/w/api.php?action=login&format=xml'
username = 'user'
password = 'password'

user_data = [('lgname', username), ('lgpassword', password)]

#Login step 1
#Make the POST request
request = urllib2.Request(url)
data = urllib.urlencode(user_data)
login_raw_data1 = urllib2.urlopen(request, data).read()

#Parse the XML for the login information
login_data1 = xml.etree.ElementTree.fromstring(login_raw_data1)
login_tag = login_data1.find('login')
token = login_tag.attrib['token']
cookieprefix = login_tag.attrib['cookieprefix']
sessionid = login_tag.attrib['sessionid']

#Set the cookies
cookie = Cookie.SimpleCookie()
cookie[cookieprefix + '_session'] = sessionid

#Login step 2
request = urllib2.Request(url)
session_cookie_header = cookieprefix+'_session='+sessionid+'; path=/; domain=.wikipedia.org; HttpOnly'

request.add_header('Set-Cookie', session_cookie_header)
user_data.append(('lgtoken', token))
data = urllib.urlencode(user_data)

login_raw_data2 = urllib2.urlopen(request, data).read()

I think the problem is somewhere in the request.add_header('Set-Cookie', session_cookie_header) line, but I don't know for sure. How do I use these python libraries to send cookies in the header with every request (which is necessary for a lot of API functions).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-12-08 00:47:47

最新版本的 requests 支持会话(而且使用起来非常简单)一般都很棒):

with requests.session() as s: 
    s.post(url, data=user_data)
    r = s.get(url_2)

The latest version of requests has support for sessions (as well as being really simple to use and generally great):

with requests.session() as s: 
    s.post(url, data=user_data)
    r = s.get(url_2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文