zoukankan      html  css  js  c++  java
  • python urllib2使用心得

    python urllib2使用心得

     

    1、http GET请求

    过程:获取返回结果,关闭连接,打印结果

    f = urllib2.urlopen(req, timeout=10)
    the_page = f.read()
    f.close()
    print the_page
    

    2、http GET请求 + 包头

    paras = "Token=1234567890;Uuid=0987654321"
    url = http://www.cnblogs.com/shhnwangjian
    send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
    req = urllib2.Request(url, headers=send_headers)  # 生成页面请求的完整数据
    f = urllib2.urlopen(req, timeout=10)  # 发送页面请求
    the_page = f.read()
    f.close()
    print the_page

    3、http GET请求 + 包头,处理返回响应包头

    paras = "Token=1234567890;Uuid=0987654321"
    send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
    url = http://www.cnblogs.com/shhnwangjian
    req = urllib2.Request(url, headers=send_headers) 
    f = urllib2.urlopen(req, timeout=10) 
    response_head = f.info().getheader("Content-Type")  # 获取返回包头中Content-Type内容
    print response_head
    f.close()


     

    4、http POST请求

    postdata = urllib.urlencode({"username": "test", "password": "123456", "type": "Z"})
    f = urllib2.urlopen(url, postdata, timeout=10)
    the_page = f.read()
    f.close()
    print the_page

    5、http POST请求 + 包头

    postdata = urllib.urlencode({"username": "test", "password": "123456", "type": "Z"})
    paras = "Token=1234567890;Uuid=0987654321"
    url = http://www.cnblogs.com/shhnwangjian
    send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
    req = urllib2.Request(url, headers=send_headers)  # 包头
    f = urllib2.urlopen(req, postdata, timeout=10)
    the_page = f.read()
    f.close()
    print the_page
    

    6、http POST请求 + 包头,处理返回响应包头

    postdata = urllib.urlencode({"username": "test", "password": "123456", "type": "Z"})
    paras = "Token=1234567890;Uuid=0987654321"
    url = http://www.cnblogs.com/shhnwangjian
    send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
    req = urllib2.Request(url, headers=send_headers)  # 包头
    f = urllib2.urlopen(req, postdata, timeout=10)
    response_head = f.info().getheader("Cookie")  # 获取返回包头中Cookie内容
    print response_head
    f.close()
    

    7、http post 请求,json数据包

    send_headers = {"User-Agent": "shhnwangjian", "Content-Type": "application/json"}
    url = http://www.cnblogs.com/shhnwangjian
    body = json.dumps({
    	"version": 1,
    	"sblx": "1",
    	"yjxx": "wangjian_AUTH",
    	"token": token,
    	"filemd5": "",
    	"crc": 123
    })
    req = urllib2.Request(url, data=body, headers=send_headers)
    f = urllib2.urlopen(req, timeout=2)
    the_page = f.read()
    f.close()

    8、基于Basic access authentication的http请求,如调用RabbitMQ API接口

    实现方式一:请求头中添加Authorization

    Basic access authentication介绍: https://en.wikipedia.org/wiki/Basic_access_authentication

    import base64
    import urllib2
    
    url = http://ip:port/api/channels
    basicpwd = base64.b64encode("guest:guest")  # 账号密码
    send_headers = {"User-Agent": "TEST", "Content-Type": "application/json", "Authorization": "Basic %s" % basicpwd}
    req = urllib2.Request(url, headers=self.head)
    f = urllib2.urlopen(req, timeout=6)
    the_page = f.read()
    f.close()

    实现方式二:http://guest:guest@ip:port/api/channels 

    http包头、包体的学习参考:http://www.cnblogs.com/shhnwangjian/p/5110304.html

  • 相关阅读:
    [MacOS]Sublime text3 安装(一)
    [RHEL8]开启BBR
    PAT Advanced 1136 A Delayed Palindrome (20分)
    PAT Advanced 1144 The Missing Number (20分)
    PAT Advanced 1041 Be Unique (20分)
    PAT Advanced 1025 PAT Ranking (25分)
    PAT Advanced 1022 Digital Library (30分)
    PAT Advanced 1019 General Palindromic Number (20分)
    PAT Advanced 1011 World Cup Betting (20分)
    PAT Advanced 1102 Invert a Binary Tree (25分)
  • 原文地址:https://www.cnblogs.com/shhnwangjian/p/5341483.html
Copyright © 2011-2022 走看看