-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathShowapiRequest.py
More file actions
executable file
·55 lines (45 loc) · 1.72 KB
/
ShowapiRequest.py
File metadata and controls
executable file
·55 lines (45 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
from urllib import parse
#全局请求头
files = {}
headers = {}
body = {}
timeouts = {}
resHeader = {}
class ShowapiRequest:
def __init__(self, url, my_appId, my_appSecret):
self.url = url
self.my_appId = my_appId
self.my_appSecret = my_appSecret
body["showapi_appid"] = my_appId
body["showapi_sign"] = my_appSecret
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2427.7 Safari/537.36"
def addFilePara(self, key, value_url):
files[key] = open(r"%s" % (value_url), 'rb')
return self
def addHeadPara(self, key, value):
headers[key] = value
return self
def addBodyPara(self, key, value):
body[key] = value
return self
#设置连接时间和读取时间
def setTimeout(self, connecttimout, readtimeout):
timeouts["connecttimout"] = connecttimout
timeouts["readtimeout"] = readtimeout
return self
def get(self):
get_url = self.url + "?" + parse.urlencode(body)
if not timeouts:
res = requests.get(get_url, headers=headers)
else:
timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
res = requests.get(get_url, headers=headers, timeout=timeouts)
return res
def post(self):
if not timeouts:
res = requests.post(self.url, files=files, data=body, headers=headers)
else:
timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
res = requests.post(self.url, files=files, data=body, headers=headers, timeout=timeout)
return res