一、Python HTTP GET请求
import requests
def http_get():
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
# Requests 中也有一个内置的 JSON 解码器,助你处理 JSON 数据
return r.json()
二、Python HTTP POST请求
1、
def http_post()
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
return json.loads(r.text)
2、POST一个多部分编码(Multipart-Encoded)的文件
def http_post_file():
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)
return json.loads(r.text)
参考: