提交 fa77327d 编写于 作者: J jiafeng

add 2 function in Requests.py

上级 83fcb89f
......@@ -120,6 +120,111 @@ class Request:
else:
return response_dicts
def post_request_multipart(self, url, data, header, file_parm, file, f_type):
"""
提交Multipart/form-data 格式的Post请求
:param url:
:param data:
:param header:
:param file_parm:
:param file:
:param type:
:return:
"""
if not url.startswith('http://'):
url = '%s%s' % ('http://', url)
print(url)
try:
if data is None:
response = requests.post(url=url, headers=header)
else:
data[file_parm] = os.path.basename(file), open(file, 'rb'), f_type
enc = MultipartEncoder(
fields=data,
boundary='--------------' + str(random.randint(1e28, 1e29 - 1))
)
header['Content-Type'] = enc.content_type
response = requests.post(url=url, params=data, headers=header)
except requests.RequestException as e:
print('%s%s' % ('RequestException url: ', url))
print(e)
return ()
except Exception as e:
print('%s%s' % ('Exception url: ', url))
print(e)
return ()
# time_consuming为响应时间,单位为毫秒
time_consuming = response.elapsed.microseconds/1000
# time_total为响应时间,单位为秒
time_total = response.elapsed.total_seconds()
Common.Consts.STRESS_LIST.append(time_consuming)
response_dicts = dict()
response_dicts['code'] = response.status_code
try:
response_dicts['body'] = response.json()
except Exception as e:
print(e)
response_dicts['body'] = ''
response_dicts['text'] = response.text
response_dicts['time_consuming'] = time_consuming
response_dicts['time_total'] = time_total
return response_dicts
def put_request(self, url, data, header):
"""
Put请求
:param url:
:param data:
:param header:
:return:
"""
if not url.startswith('http://'):
url = '%s%s' % ('http://', url)
print(url)
try:
if data is None:
response = requests.put(url=url, headers=header)
else:
response = requests.put(url=url, params=data, headers=header)
except requests.RequestException as e:
print('%s%s' % ('RequestException url: ', url))
print(e)
return ()
except Exception as e:
print('%s%s' % ('Exception url: ', url))
print(e)
return ()
time_consuming = response.elapsed.microseconds/1000
time_total = response.elapsed.total_seconds()
Common.Consts.STRESS_LIST.append(time_consuming)
response_dicts = dict()
response_dicts['code'] = response.status_code
try:
response_dicts['body'] = response.json()
except Exception as e:
print(e)
response_dicts['body'] = ''
response_dicts['text'] = response.text
response_dicts['time_consuming'] = time_consuming
response_dicts['time_total'] = time_total
return response_dicts
if __name__ == '__main__':
url = 'http://c-api-sellerbasicservice.staging.shopeemobile.com/api/fulfillment/order/get_forder_from_oms'
header = {'shop-id': '321239', 'region-id': 'SG', 'user-id': '321238', 'Content-Type': 'application/json'}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册