七、python小功能记录——get和post请求
2020-12-13 01:53
标签:span 功能 http map class urllib turn lib method 先引入包 下文中context是https方式用到 context = ssl._create_unverified_context() 1.get 2.post values = {} 3.请求结果json结构化 七、python小功能记录——get和post请求 标签:span 功能 http map class urllib turn lib method 原文地址:https://www.cnblogs.com/cvol/p/11017598.htmlimport urllib.request
import ssl
import json
request = urllib.request.Request(url)
response = urllib.request.urlopen(url=request,context=context)
dic = response.read().decode(‘utf-8‘)
headers = {}
headers[‘Content-Type‘] = ‘application/json; charset=utf-8‘#j_data是json结构化数据
values["token"] =token
values["CustomApp"]="Web"j_data = json.dumps(values)#结构化数据之后才发送请求
def jsonPost(url,j_data):
request = urllib.request.Request(url,bytes(j_data,‘utf8‘), headers,method=‘POST‘)#bytes:把请求内容转成bytes才能发送,否则报错
response = urllib.request.urlopen(url=request,context=context)
dic = response.read().decode(‘utf-8‘)
response.close()
return dicjs = json.loads(dic)
上一篇:java-使用反射实现ORM映射