python3之request用法_python3 中的http.client,urlib.request,以及三方库requests发送请求的用法...

http.client 的用法

1、get请求,不带参数

import http.client

# 1. 建立HTTP连接

conn = http.client.HTTPConnection("www.wushuai.net")

# 2. 发送GET请求,制定接口路径

conn.request("GET", '/get')

# 3. 获取响应

res = conn.getresponse()

# 4. 解析响应.进行解码

print(res.read().encode("utf-8")) # 自己解码

2、get请求,带中文参数

python

import http.client

import urllib.parse

conn = http.client.HTTPSConnection("www.wushuai.net")

url = urllib.parse.quote("/get?name=张三&age=12", safe=':/?=&')

# 上面quote() 是把链接中的特殊字符进行url编码

conn.request("GET", url)

res = conn.getresponse()

print(res.read().decode("utf-8")) # 自己解码

3、post 请求,普通表单参数

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"))

4、post 请求,json

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"}

# 请求头,方式为json

conn.request("POST", '/post', data, headers)

res = conn.getresponse()

print(res.read().decode("utf-8"))

urlib.request的用法

1、 get请求,不带参数

import urllib

res = urllib.request.urlopen("http://httpbin.org/get")

print(res.read().decode("utf-8")) # 自己解码

get请求,带中文参数

import urllib

import urllib.parse

url = urllib.parse.quote("https://www.wushuai.net/get?name=张三&age=12",safe=':/?=&')

# 进行url编码

res = urllib.request.urlopen("url")

print(res.read().decode("utf-8")) # 自己解码

3、post 请求,普通表单参数

import urllib

import urllib.parse

import urllib.request

data = urllib.parse.urlencode({"name":"张三", "age": 12}).encode("utf-8")

# 对数据进行url编码及utf-8编码

# response = urllib.request.Request(url=url, data=data, headers=headers, method='POST')

req = urllib.request.Request("https://www.wushuai.net/", data=data)

res = urllib.request.urlopen(req)

print(res.read().decode("utf-8"))

4、post 请求,json

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("https://www.wushuai.net/", data=data, headers=headers,method='POST')

res = urllib.request.urlopen(req)

print(res.read().decode("utf-8"))

requests 的用法

1、 get请求,不带参数

import requests

res = requests.get("https://www.wushuai.net/get")

print(res.text) # 自动按默认utf-8解码

2、get请求,带中文参数

import requests

res = requests.get("https://www.wushuai.net/get?name=张三&age=12") # 自动编码

print(res.text) # 自动按默认utf-8解码

3、post 请求,普通表单参数

import requests

data = {"name":"张三", "age": 12}

res = requests.post("https://www.wushuai.net/post", data=data) # 自动编码

print(res.text)

4、post 请求,json

import requests

data = {"name":"张三", "age": 12}

res = requests.post("https://www.wushuai.net/post", json=data)

print(res.json()) # 转为字典格式

或者

import requests

import json

data = {"name":"张三", "age": 12}

headers = {"Content-Type": "application/json"}

res = requests.post("https://www.wushuai.net/post", data=json.dumps(data), headers=headers)

print(res.json()) # 转为字典格式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值