Use cookielib in Python to simulate website login, pythoncookielib

Source: Internet
Author: User

Use cookielib in Python to simulate website login, pythoncookielib

The Python login Simulated Program is briefly mentioned above, but it is not clearly written. Here we will add a Python login simulated sample program with annotations. To put it simply, use cookielib to obtain the cookie, and then use the obtained cookie to enter the website to be logged on.

#-*-Coding: UTF-8 -*-#! /Usr/bin/python import urllib2 import urllib import cookielib import re auth_url = 'HTTP: // www.nowamagic.net/'home_url = 'HTTP: // www.nowamagic.net/'; # login username and password data = {"username": "nowamagic", "password": "pass"} # urllib encoding post_data = urllib. urlencode (data) # Sending header information headers = {"Host": "www.nowamagic.net", "Referer": "http://www.nowamagic.net"} # initialize a CookieJar to process Cookie cookieJar = cookielib. cookieJar () # instantiate a global opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (cookieJar) # obtain cookie req = urllib2.Request (auth_url, post_data, headers) result = opener. open (req) # access the home page automatically with the cookie information result = opener. open (home_url) # display the result print result. read ()

A few examples are included:

1. Use existing cookies to access the website

import cookielib, urllib2  ckjar = cookielib.MozillaCookieJar(os.path.join('C:\Documents and Settings\tom\Application Data\Mozilla\Firefox\Profiles\h5m61j1i.default', 'cookies.txt'))  req = urllib2.Request(url, postdata, header)  req.add_header('User-Agent', \   'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(ckjar) )  f = opener.open(req) htm = f.read() f.close()

2. access the website to obtain the cookie and save it in the cookie file.

 import cookielib, urllib2  req = urllib2.Request(url, postdata, header) req.add_header('User-Agent', \   'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')  ckjar = cookielib.MozillaCookieJar(filename) ckproc = urllib2.HTTPCookieProcessor(ckjar)  opener = urllib2.build_opener(ckproc)  f = opener.open(req) htm = f.read() f.close()  ckjar.save(ignore_discard=True, ignore_expires=True)

3. Use the specified parameter to generate a cookie and use the cookie to access the website.

 import cookielib, urllib2  cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) values = {'redirect':", 'email':'abc@abc.com',      'password':'password', 'rememberme':", 'submit':'OK, Let Me In!'} data = urllib.urlencode(values)  request = urllib2.Request(url, data) url = urlOpener.open(request) print url.info() page = url.read()  request = urllib2.Request(url) url = urlOpener.open(request) page = url.read() print page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.