zoukankan      html  css  js  c++  java
  • Python3 内置http.client,urllib.request及三方库requests发送请求对比

    如有任何学习问题,可以添加作者微信:lockingfree
    更多学习资料请加QQ群: 822601020获取

    HTTP,GET请求,无参

    GET http://httpbin.org/get

    Python3 http.client

    import http.client
    
    # 1. 建立HTTP连接
    conn = http.client.HTTPConnection("httpbin.org")
    # 2. 发送GET请求,制定接口路径
    conn.request("GET", '/get')
    # 3. 获取相应
    res = conn.getresponse()
    # 4. 解析相应.进行解码
    print(res.read().encode("utf-8")) # 自己解码
    

    Python3 urllib.request

    import urllib.request  # 如果import urllib,则在使用urllib.request时会报错
    
    res = urllib.request.urlopen("http://httpbin.org/get")
    print(res.read().decode("utf-8"))  # 自己解码
    
    

    Python3 requests

    import requests
    
    res = requests.get("http://httpbin.org/get")
    print(res.text) # 自动按默认utf-8解码
    

    HTTPS,GET请求,带中文参数

    GET http://httpbin.org/get?name=张三&age=12

    Python3 http.client

    import http.client
    import urllib.parse
    
    conn = http.client.HTTPSConnection("httpbin.org")
    url = urllib.parse.quote("/get?name=张三&age=12", safe=':/?=&') # 进行url编码
    conn.request("GET", url)
    res = conn.getresponse()
    print(res.read().decode("utf-8")) # 自己解码
    

    Python3 urllib.request

    import urllib
    import urllib.parse
    
    url = urllib.parse.quote("https://httpbin.org/get?name=张三&age=12", safe=':/?=&') # 进行url编码
    res = urllib.request.urlopen("url")
    print(res.read().decode("utf-8"))  # 自己解码
    
    

    Python3 requests

    import requests
    
    res = requests.get("https://httpbin.org/get?name=张三&age=12") # 自动编码
    print(res.text) # 自动按默认utf-8解码
    

    Post x-www-form-urlencoded传统表单请求

    POST http://httpbin.org/post 请求数据: name=张三&age=12

    Python3 http.client

    import http.client
    import urllib.parse
    
    conn = http.client.HTTPConnection("httpbin.org")
    data = urllib.parse.urlencode({"name":"张三", "age": 12}).encode("utf-8") # 对数据进行url编码及utf-8编码
    conn.request("POST", '/post', data)
    res = conn.getresponse()
    print(res.read().decode("utf-8"))
    

    Python3 urllib.request

    import urllib
    import urllib.parse
    import urllib.request
    
    data = urllib.parse.urlencode({"name":"张三", "age": 12}).encode("utf-8") # 对数据进行url编码及utf-8编码
    req = urllib.request.Request("http://httpbin.org/post", data=data)
    res = urllib.request.urlopen(req)
    print(res.read().decode("utf-8")) 
    
    

    Python3 requests

    import requests
    
    data = {"name":"张三", "age": 12}
    res = requests.post("http://httpbin.org/post", data=data) # 自动编码
    print(res.text)
    

    Post application/json请求

    POST http://httpbin.org/post 请求数据: {"name": "张三","age": 12}

    Python3 http.client

    import http.client
    import urllib.parse
    import json
    
    conn = http.client.HTTPConnection("httpbin.org")
    data = '{"name":"张三", "age": 12}'.encode('utf-8') # 或data = json.dumps({"name":"张三", "age": 12})
    headers = {"Content-Type": "application/json"}
    conn.request("POST", '/post', data, headers)
    res = conn.getresponse()
    print(res.read().decode("utf-8"))
    

    Python3 urllib.request

    import urllib
    import urllib.parse
    import urllib.request
    import json
    
    data = '{"name":"张三", "age": 12}'.encode('utf-8') # 或data = json.dumps({"name":"张三", "age": 12})
    headers = {"Content-Type": "application/json"}
    req = urllib.request.Request("http://httpbin.org/post", data=data, headers=headers)
    res = urllib.request.urlopen(req)
    print(res.read().decode("utf-8")) 
    
    

    Python3 requests

    import requests
    
    data = {"name":"张三", "age": 12}
    res = requests.post("http://httpbin.org/post", json=data)
    print(res.json())  # 转为字典格式
    

    import requests
    import json
    
    data = {"name":"张三", "age": 12}
    headers = {"Content-Type": "application/json"}
    res = requests.post("http://httpbin.org/post", data=json.dumps(data), headers=headers)
    print(res.json())  # 转为字典格式
    
  • 相关阅读:
    学习笔记180—回归系数与相关系数的关系和区别
    学习笔记178—精品书籍推荐榜
    学习笔记177—PPT生成的图片设置成特定像素级的图片【四种方法】
    学习笔记176—PS 获得一个椭圆的某个部分
    shell:利用sed删除文件中的匹配行
    常用壁纸
    Linux下编译C文件:Hello World
    属性访问、特性和修饰符
    介绍Python基本特殊方法
    kafka配置
  • 原文地址:https://www.cnblogs.com/superhin/p/11455240.html
Copyright © 2011-2022 走看看