# -*- coding: utf-8 -*- # @Time : 2021/9/05 13:35 # @Author : jiafeng.li # @File : Request.py ''' 封装request ''' import json import os import random import requests import Common.Consts from requests_toolbelt import MultipartEncoder class Request: def __init__(self): pass def Post_Result(self, url, data, header): ''' Post请求 :param url: :param data: :return: ''' if not url.startswith('http://'): url = '%s%s' % ('http://', url) print(url) data = json.dumps(data) #header = json.dumps(header) try: if data is None: response = requests.post(url=url, headers=header) else: response = requests.post(url=url, data=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() Common.Consts.RESULT_LIST.append('True') except Exception as e: print(e) response_dicts['body'] = '' Common.Consts.RESULT_LIST.append('False') response_dicts['text'] = response.text response_dicts['time_consuming'] = time_consuming response_dicts['time_total'] = time_total #return json.dumps(response_dicts) return response_dicts def Get_Request(self, url, data, header, download_file=False): """ Get请求 :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.get(url=url, headers=header) else: response = requests.get(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 if download_file == True: return response else: 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'} data = 'order_id=85722908295387' a = Request() result = a.Get_Request(url, data, header) print(json.dumps(result))