传递会话cookie在http头与python urllib2? [英] pass session cookies in http header with python urllib2?
问题描述
我试图写一个简单的脚本来登录维基百科,并在我的用户页面上使用Mediawiki API执行一些操作。但是,我似乎没有超过第一个登录请求(从此页: https:/ /en.wikipedia.org/wiki/Wikipedia:Creating_a_bot#Logging_in )。我不认为我设置的会话cookie正在发送。这是我的代码到目前为止:
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()
我认为问题是在 request.add_header('Set-Cookie',session_cookie_header)
行的某处,但我不知道肯定。如何使用这些python库在每个请求的标头中发送cookie(这对于许多API函数是必需的)。
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).
推荐答案
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)
这篇关于传递会话cookie在http头与python urllib2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!