python recurrent-platform
https://www.pythonforbeginners.com/requests/using-requests-in-python
//you can relay on HTTP requests,like PUT,DELETE,HEAD,and OPTION
r=requests.put("http://httpbin.org/put")
r=requests.delete("http://httpbin.org/delete")
r=requests.head("http://httpbin.org/get")
r=requests.option("http://httpbin.org/get")
http://docs.python-requests.org/en/master/user/quickstart/
import json
url="https://api.github.com/some/endpoint"
payload={"some":"data"}
r=requests.post(url,data=json.dumps(payload))//将 Python 对象编码成 JSON 字符串
//Instead of encoding the dict(payload) yourself,you can also pass it directly using the json parameter and it will be encoded automatically.
//由于传过去的数据我写成了data=payload导致报错:需要json格式的数据,所以写成json=payload
r=requests.post(url,json=payload)
//using the json parameter in the request will change the Content-Type in the header to application/json.
https://blog.csdn.net/shanzhizi/article/details/50903748
r.status_code #响应状态码
r.raw #返回原始响应体,也就是urllib的response对象,使用r.raw.read()读取
r.content #字节方式的响应体,会自动为你解码gzip和deflate压缩
r.text #字符串方式的响应体,会自动根据响应头部的字符编码进行解码
r.headers #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
r.json() #Requests中内置的JSON解码器
r.raise_for_status() #失败请求(非200响应)抛出异常
import requests
URL="http://ip.taobao.com/service/getIpInfo.php"
try:
r=requests.get(URL,params={"ip":"8.8.8.8"},timeout=1)
r.raise_for_status() #如果响应状态码不是200,就主动抛出异常
except requests.RequestException as e:
print(e)
else:
result=r.json()
print(type(result),result,sep="\n")
上传图片
import requests
url = 'http://127.0.0.1:5000/upload'
files = {'file': open('/home/lyb/sjzl.mpg', 'rb')}
#files = {'file': ('report.jpg', open('/home/lyb/sjzl.mpg', 'rb'))} #显式的设置文件名
r = requests.post(url, files=files)
print(r.text)
put:增加,delete:删除
基本上都可以用get/post解决,所以用的少