import requests
import json
class HttpCommon:
def get(self,url,encode):
headers = {
"Connection": "keep-alive",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"User-Agent": "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Content-Type": "application/x-www-form-urlencoded; charset="+encode.upper(),
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9"
}
res=requests.get(url,headers=headers)
res.encoding=encode
return res.text
def post(self,url,param,encode):
headers = {
"Connection": "keep-alive",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"User-Agent": "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Content-Type": "application/x-www-form-urlencoded; charset="+encode.upper(),
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9"
}
res = requests.post(url, data=json.dumps(param), headers=headers)
res.encoding = encode
return res.text
def postjson(self,url,param):
headers = {
"Connection": "keep-alive",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"User-Agent": "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Content-Type": "application/json",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9"
}
# param 参数为 {"appid": "shpt","appsecret": "7e03a331-0407-4591-ba7f-75ee08756d4f"}
res = requests.post(url, data=json.dumps(param), headers=headers)
res.encoding = "utf-8"
return res.text
python 基于 requests 发送 HTTP 请求
最新推荐文章于 2024-09-05 10:14:29 发布
本文介绍了一个通用的HTTP请求处理类,包括GET和POST请求的方法,使用Python的requests库实现。详细展示了如何设置请求头,处理编码,并发送JSON参数。
4537

被折叠的 条评论
为什么被折叠?



