python urllib2를 사용하여 http 헤더에 세션 쿠키 전달 ?

미디어 위키 API를 사용하여 Wikipedia에 로그인하고 내 사용자 페이지에서 몇 가지 작업을 수행하는 간단한 스크립트를 작성하려고합니다. 그러나 첫 번째 로그인 요청을 통과하지 못한 것 같습니다 (이 페이지에서 : https://en.wikipedia.org/wiki/Wikipedia:Creating_a_bot#Logging_in ). 세션 쿠키가 아닌 것 같습니다. 내가 설정 한 내용이 전송되고 있습니다. 이것은 지금까지 내 코드입니다.

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) 줄 어딘가에 있다고 생각하지만 전송하지 않습니다. 이 파이썬 라이브러리를 사용하여 모든 요청 (많은 API 기능에 필요함)과 함께 헤더에 쿠키를 보내려면 어떻게해야합니까?

7
задан Krenair 1 December 2016 в 01:21
поделиться