一、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

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())  # 转为字典格式

作者:韩志超
链接:https://www.jianshu.com/p/491d6590b2a0

Python3 内置http.client,urllib.request及三方库requests发送请求对比相关推荐

  1. python3 内置函数map 返回的迭代器转为列表

    python3 内置函数map 返回的是迭代器,python2 返回的才是列表, 在python3中想要使用需要把迭代器在转换成列表 list_a = [1, 2, 3, 4, 5]def add(n ...

  2. python使用osgeo库_Python使用内置urllib模块或第三方库requests访问网络资源

    前言 更多内容,请访问我的 个人博客. Python 访问网络资源有很多方法,urllib, urllib2, urllib3, httplib, httplib2, requests ,现介绍如下两 ...

  3. python3内置函数详解

    原文链接:https://www.cnblogs.com/xiao1/p/5856890.html 菜鸟教程:https://www.runoob.com/python/python-built-in ...

  4. Python3内置函数大全,文章有点长请耐心一点哦

    1.abs()函数 ''' abs() 函数返回数字的绝对值. 绝对值:absolute 正如字面上的意思,可以返回一个绝对值 ''' import math print('abs(45)的值:',a ...

  5. python3内置函数大全

    1.abs()函数 ''' abs() 函数返回数字的绝对值. 绝对值:absolute 正如字面上的意思,可以返回一个绝对值 ''' import math print('abs(45)的值:',a ...

  6. python3 内置函数

    """ # 内置函数之---abs():绝对值 # a = 10086 a = 'hello' try:print(abs(a))with open('666.p', ' ...

  7. 利用Python3内置文档资源高效学习及官方中文文档

    概述 从前面的对Python基础知识方法介绍中,我们几乎是围绕Python内置方法进行探索实践,比如字符串.列表.字典等数据结构的内置方法,和大量内置的标准库,诸如functools.time.thr ...

  8. python3 内置函数详解

    内置函数详解 abs(x) 返回数字的绝对值,参数可以是整数或浮点数,如果参数是复数,则返回其大小. # 如果参数是复数,则返回其大小.>>> abs(-25) 25>> ...

  9. python3内置函数_python3--内置函数

    python3--内置函数 内置函数: 截止到python 3.6.2 版本,现在python一共提供了68个内置函数:即python提供给你直接可以拿来使用的所有函数. (红色字体为重点掌握内置函数 ...

最新文章

  1. 中国投稿第一!ACL2021开幕,历届最大审稿团,预训练刷屏
  2. 【深度学习】神经网络结构搜索(NAS)与多模态
  3. RHEL在戴尔系统上p1p1 ......命名规则
  4. sql2005还原出现“受限制用户”解决方法
  5. Vue 犯罪指南:TypeError: Right-hand side of 'instanceof' is not an object
  6. 从Myeclipe转向Idea,各种遇坑与填坑经验,持续更新(图文)
  7. 月薪仅18K的NLP工程师,回炉重造吧!
  8. com scripting读书笔记
  9. Bootstraphead里的内容
  10. ERROR 1045 (28000): Access denied for user 'root'@'localhost' 的解决方法
  11. 一个比Profiler和Netch更好用的软件代理加速工具
  12. 复制粘贴神器allow copy
  13. IDEA 运行 Tomcat 中文乱码的问题
  14. 用Python计算现在距离春节还剩多长时间
  15. 关于计算机未来理想,关于未来与梦想的作文
  16. FPGA三分频,五分频,奇数分频
  17. 黑马程序员——Java语言基础——关键字、表示符、常量、变量及数据的类型、运算符
  18. 深度学习-扩张卷积(dilated convolution)
  19. 利用pandas对在链家网爬取的租房数据进行清洗
  20. linux命令~iconv

热门文章

  1. ajax 载入html后不能执行其中的js解决方法
  2. RadioButton 自定义控件
  3. iOS Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)
  4. Android图片360全景旋转
  5. 分布式检索系统的简单设计
  6. [译] Facebook杯2013年编程挑战赛——第一轮题目及答案
  7. 我该如何学习spring源码以及解析bean定义的注册
  8. P3482 [POI2009]SLO-Elephants
  9. 超图软件:以用户价值为源点做精准化的研发
  10. apache 创建多端口监听