Basic Learning of cookielib Module

Source: Internet
Author: User

#-*-Coding: UTF-8 -*-

# Python: 2.x

_ Author _ = 'admin'

Import cookielib

# Mainly used to process HTTP client cookies

 

# Cookielib. loaderror fails to be loaded in an exception file, which is a subclass of ioeerror.

# Cookielib. cookiejar is used to store Cookie objects. This module captures cookies and resends them when you are asking for further connection information. It can also be used to process files containing cookie data.

# Documentation: https://docs.python.org/2/library/cookielib.html? Highlight = cookielib # cookielib

"""

The module provides the following objects: cookiejar, filecookiejar, mozillacookiejar, and lwpcookiejar.

Cookiejar objects are stored in the memory.

Is filecookiejar an object implemented? cookiepolicy Interface

Mozillacookiejar is used to load and store cookies in the file format used by the disk mozillacookies.txt.

Lwpcookiejar can load and save cookies to disk formats compatible with libwww-perl library Set-Cookie3 file formats

"""

Import urllib2, urllib

Cj = cookielib. cookiejar ()

Openner = urllib2.build _ opener (urllib2.httpcookieprocessor (CJ ))

R = openner. Open ('HTTP: // www.baidu.com ')

Print Dir (r)

# Example: Python simulation to obtain the OSC openapi authorization_code :( users provide) http://www.oschina.net/code/snippet_1244912_38304

 

Class myweb ():

Def _ init _ (Self ):

Self. header = {"User-Agent": "Opera/8.0 (Macintosh; PPC Mac OS X; U; en )",

"Referer": "https://www.oschina.net/action/oauth2/authorize? \

Response_type = Code & client_id = lew1z8ylznhyit7sb1kz &\

Redirect_uri = http://www.oschina.net /"}

Self. Cookie = cookielib. lwpcookiejar () # Can load and save cookies to disk formats compatible with the libwww-perl library Set-Cookie3 file formats

Self. cookie_support = urllib2.httpcookieprocessor (self. Cookie) # process HTTP cookies

Self. Opener = urllib2.build _ opener (self. cookie_support, urllib2.httphandler) # generate an opener and access the URL through the opener; httphandler: process the HTTP URL

Urllib2.install _ opener (self. opener) # Use urllib2.install _ opener (opener) to add opener that requires global applications to urllib2.

Def post (self, posturl, dictdata ):

'''

Pass two arguement, posturl and postdict

Return urllib2.urlopen (request)

'''

Posdata = urllib. urlencode (dictdata)

Request = urllib2.request (posturl, posdata, self. header) # add a specific headerurllib2.request (URL [, data] [, headers] [, origin_req_host] [, unverifiable]) to the request.

Try:

Content = urllib2.urlopen (request)

Except t exception, E:

Print STR (E)

Print type (content)

Return content

 

If _ name __= = '_ main __':

Import hashlib

Web = myweb ()

Password =''

Email =''

Password = hashlib. sha1 (password). hexdigest ()

Dictdata = {

'Email ': email,

'Pwd': Password

}

Url = "https://www.oschina.net/action/user/hash_login"

Urlauth = "https://www.oschina.net/action/oauth2/authorize"

 

Dictdata2 = {

'Client _ id': 'lew1z8ylznhyit7sb1kz', # requested client_id

'Response _ type': 'code ',

'Redirect _ Uris ': 'http: // www.oschina.net /',

'Scope ': 'blog _ API, comment_api, favorite_api, message_api ,\

News_api, notice_api, post_api, search_api ,\

Tweet_api, user_api, user_mod_api ,',

'State ':'',

'User _ oauth_approval ': 'true ',

'Email ': email,

'Pwd': Password

}

Web. Post (URL, dictdata)

TMP = web. Post (urlauth, dictdata2)

Print TMP. url

Basic Learning of cookielib Module

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.