python - How do i implement cookielib to create a session -
import urllib2 import urllib url = "http://www.torn.com/authenticate.php" username = raw_input("your username; ") password = raw_input("your password: ") query = {'player':username, 'password':password} data_query = urllib.urlencode(query) sending_data = urllib2.request(url, data_query) print sending_data() response = urllib2.urlopen(sending_data) print "you logged unto to:", response.geturl() print response.read()
how implement cookielib create session , please explain line line thank you
from cookielib import cookiejar cj = cookiejar() login_data = urllib.urlencode({'j_username' : username, 'j_password' : password}) opener = urllib2.build_opener(urllib2.httpcookieprocessor(cj)) opener.open("http://www.torn.com/authenticate.php", login_data)
first, want initialize cookiejar. encode credentials, need sent in form read php server. have initialize opener, http client, , configure use cookiejar. submit login info server create session , generate cookies. continue using these cookies, use opener.open()
instead of urllib2.urlopen
(although can still use urllib2.request()
generate requests.
Comments
Post a Comment