This article mainly shares a simple encapsulation code of the python webpage request module urllib2. if you have any need, refer to the simple encapsulation of the python webpage request module urllib2.
Example:
The code is as follows:
#! /Usr/bin/python
# Coding: UTF-8
Import base64
Import urllib
Import urllib2
Import time
Class SendRequest:
'''
This class use to set and request the http, and get the info of response.
E.g. set Authorization Type, request tyep ..
E.g. get html content, state code, cookie ..
SendRequest ('http: // 10.75.0.103: 8850/2/photos/square/type. json ',
Data = 'source = 216274069 ', type = 'post', auth = 'base ',
User = 'zl2010', password = '123 ')
'''
Def _ init _ (self, url, data = None, type = 'get', auth = None, user = None, password = None, cookie = None, ** header ):
'''
Url: request, raise error if none
Date: data for post or get, must be dict type
Type: GET, POST
Auth: option, if has the value must be 'base' or 'cookie'
User: user for auth
Password: password for auth
Cookie: if request with cookie
Other header info:
E.g. referer = 'www .sina.com.cn'
'''
Self. url = url
Self. data = data
Self. type = type
Self. auth = auth
Self. user = user
Self. password = password
Self. cookie = cookie
If 'referer' in header:
Self. referer = header [referer]
Else:
Self. referer = None
If 'user-agent' in header:
Self. user_agent = header [user-agent]
Else:
Self. user_agent = None
Self. setup_request ()
Self. send_request ()
Def setup_request (self ):
'''
Setup a request
'''
If self. url = None or self. url = '':
Raise 'The url shocould not empty! '
# Set request type
# Print self. url
# Print self. type
# Print self. data
# Print self. auth
# Print self. user
# Print self. password
If self. type = 'Post ':
Self. Req = urllib2.Request (self. url, self. data)
Elif self. type = 'get ':
If self. data = None:
Self. Req = urllib2.Request (self. url)
Else:
Self. Req = urllib2.Request (self. url + '? '+ Self. data)
Else:
Print 'The http request type NOT support now! '
# Set auth type
If self. auth = 'base ':
If self. user = None or self. password = None:
Raise 'The user or password was not given! '
Else:
Auth_info = base64.encodestring (self. user + ':' + self. password). replace ('\ n ','')
Auth_info = 'basic '+ auth_info
# Print auth_info
Self. Req. add_header ("Authorization", auth_info)
Elif self. auth = 'cooker ':
If self. cookie = None:
Raise 'The cookie was not given! '
Else:
Self. Req. add_header ("Cookie", self. cookie)
Else:
Pass ## add other auth type here
# Set other header info
If self. referer:
Self. Req. add_header ('referer', self. referer)
If self. user_agent:
Self. Req. add_header ('User-agent', self. user_agent)
Def send_request (self ):
'''
Send a request
'''
# Get a response object
Try:
Self. Res = urllib2.urlopen (self. Req)
Self. source = self. Res. read ()
Self. goal_url = self. Res. geturl ()
Self. code = self. Res. getcode ()
Self. head_dict = self.Res.info (). dict
Self. Res. close ()
Failed T urllib2.HTTPError, e:
Self. code = e. code
Print e
Def get_code (self ):
Return self. code
Def get_url (self ):
Return self. goal_url
Def get_source (self ):
Return self. source
Def get_header_info (self ):
Return self. head_dict
Def get_cookie (self ):
If 'set-cookies' in self. head_dict:
Return self. head_dict ['set-cooker']
Else:
Return None
Def get_content_type (self ):
If 'content-type' in self. head_dict:
Return self. head_dict ['content-type']
Else:
Return None
Def get_expires_time (self ):
If 'expires' in self. head_dict:
Return self. head_dict ['expires']
Else:
Return None
Def get_server_name (self ):
If 'server' in self. head_dict:
Return self. head_dict ['server']
Else:
Return None
Def _ del _ (self ):
Pass
_ All _ = [SendRequest,]
If _ name _ = '_ main __':
'''
The example for using the SendRequest class
'''
Value = {'source': '000000 '}
Data = urllib. urlencode (value)
Url = 'http: // 10.75.0.103: 8850/2/photos/square/type. json'
User = 'wz _ 000000'
Password = '000000'
Auth = 'base'
Type = 'post'
T2 = time. time ()
Rs = SendRequest ('http: // www.google.com ')
# Rs = SendRequest (url, data = data, type = type, auth = auth, user = user, password = password)
Print 'T2: '+ str (time. time ()-t2)
Print '--------------- get_code ()---------------'
Print rs. get_code ()
Print '--------------- get_url ()---------------'
Print rs. get_url ()
Print '--------------- get_source ()---------------'
Print rs. get_source ()
Print '--------------- get_cookie ()---------------'
Print rs. get_cookie ()
Rs = None