最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python網(wǎng)頁(yè)請(qǐng)求urllib2模塊簡(jiǎn)單封裝代碼

 更新時(shí)間:2014年02月07日 12:02:35   作者:  
這篇文章主要分享一個(gè)python網(wǎng)頁(yè)請(qǐng)求模塊urllib2模塊的簡(jiǎn)單封裝代碼,有需要的朋友參考下

對(duì)python網(wǎng)頁(yè)請(qǐng)求模塊urllib2進(jìn)行簡(jiǎn)單的封裝。

例子:

復(fù)制代碼 代碼如下:

#!/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='111111')
  '''
  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 should 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 == 'cookie':
      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()
    except 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-cookie' in self.head_dict:
      return self.head_dict['set-cookie']
    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':'216274069'}
  data = urllib.urlencode(value)
  url = 'http://10.75.0.103:8850/2/photos/square/type.json'
  user = 'wz_0001'
  password = '111111'
  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

相關(guān)文章

  • python?opencv背景減去法摳圖實(shí)現(xiàn)示例

    python?opencv背景減去法摳圖實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了python?opencv背景減去法摳圖實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python+opencv圖像分割實(shí)現(xiàn)分割不規(guī)則ROI區(qū)域方法匯總

    python+opencv圖像分割實(shí)現(xiàn)分割不規(guī)則ROI區(qū)域方法匯總

    這篇文章主要介紹了python+opencv圖像分割實(shí)現(xiàn)分割不規(guī)則ROI區(qū)域方法匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 一些常見(jiàn)Python簡(jiǎn)單算法易錯(cuò)題及答案總結(jié)

    一些常見(jiàn)Python簡(jiǎn)單算法易錯(cuò)題及答案總結(jié)

    這篇文章總結(jié)了Python編程中的25個(gè)常見(jiàn)問(wèn)題及其解答,涵蓋字符串操作、列表操作、字典操作、排序算法、日期時(shí)間處理、文件操作、異常處理等多個(gè)方面,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • pycharm使用技巧之自動(dòng)調(diào)整代碼格式總結(jié)

    pycharm使用技巧之自動(dòng)調(diào)整代碼格式總結(jié)

    這篇文章主要給大家介紹了關(guān)于pycharm使用技巧之自動(dòng)調(diào)整代碼格式總結(jié)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python參數(shù)解析模塊sys、getopt、argparse使用與對(duì)比分析

    Python參數(shù)解析模塊sys、getopt、argparse使用與對(duì)比分析

    今天小編就為大家分享一篇關(guān)于Python參數(shù)解析模塊sys、getopt、argparse使用與對(duì)比分析,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • Python變量作用域LEGB用法解析

    Python變量作用域LEGB用法解析

    這篇文章主要介紹了Python變量作用域LEGB用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Python使用matplotlib繪制Logistic曲線操作示例

    Python使用matplotlib繪制Logistic曲線操作示例

    這篇文章主要介紹了Python使用matplotlib繪制Logistic曲線操作,結(jié)合實(shí)例形式詳細(xì)分析了Python基于matplotlib庫(kù)繪制Logistic曲線相關(guān)步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-11-11
  • python如何去除字符串兩端的引號(hào)

    python如何去除字符串兩端的引號(hào)

    這篇文章主要介紹了python如何去除字符串兩端的引號(hào)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python中優(yōu)雅使用assert斷言的方法實(shí)例

    Python中優(yōu)雅使用assert斷言的方法實(shí)例

    我們?cè)陂_(kāi)發(fā)一個(gè)程序時(shí)候,與其讓它運(yùn)行時(shí)崩潰,不如在它出現(xiàn)錯(cuò)誤條件時(shí)就崩潰(返回錯(cuò)誤),這時(shí)候斷言assert就顯得非常有用,這篇文章主要給大家介紹了關(guān)于Python中優(yōu)雅使用assert斷言的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Python如何實(shí)現(xiàn)定時(shí)器功能

    Python如何實(shí)現(xiàn)定時(shí)器功能

    在本篇文章里小編給大家分享的是關(guān)于Python中的簡(jiǎn)單定時(shí)器實(shí)例及代碼,需要的朋友們可以學(xué)習(xí)下。
    2020-05-05

最新評(píng)論

东至县| 兰考县| 于都县| 景德镇市| 镇沅| 柳州市| 鄱阳县| 武安市| 淮安市| 宁波市| 平利县| 宁陵县| 肇东市| 石棉县| 樟树市| 永昌县| 故城县| 九龙坡区| 洪湖市| 东城区| 石门县| 中西区| 冕宁县| 北票市| 安徽省| 安化县| 平远县| 水富县| 嘉义市| 南涧| 深泽县| 宜兴市| 文水县| 浠水县| 瓦房店市| 平陆县| 苗栗市| 阿坝县| 石台县| 青铜峡市| 乐东|