闲来无事,把以前的代码贴出来
#__author__ = 'chubby_superman'
#_*_coding=utf-8 _*
import requests
from datetime import datetime
import json
class Httpreq():
def __init__(self,url,data,heards):
self.url=url
self.data=data
self.heards=heards
def req_1(self):
requests_statuscode=requests.post(url=self.url,json=self.data).status_code
try:
if requests_statuscode == 200:
print(1)
#return "请求成功,开始判断响应状态"
#requests_statuscode.text
else:
#return requests_statuscode
print("请求失败if内")
except Exception as e:
print("请求失败try内")
#3try:
# a = datetime.datetime()
x = requests.post(url=self.url,json=self.data)
try :
a = x.text
with open(datetime.now().strftime('%Y%m%d%H%M%S') + '.txt','w',encoding="utf-8") as f:
f.write(a)
except Exception as e:
print("写入文件有问题")
a = x.json()["code"]
if a == 1000:
print("正常返回")
elif a == -1:
print("系统繁忙")
elif a == 1001:
print("partnerId无效")
elif a == 1002:
print("IP白名单错误,禁止服务")
elif a == 1003 :
print("签名错误")
elif a == 1004 :
print("缺少必要的参数")
else:
print("返回未知的错误")
if __name__ == '__main__':
the_url='http://test.pipifit.com/analysisShopCartOverCate'
#the_data={"day": 7,"goodsInfo": [{"goodsId": 1,"goodsNumber": 1}],"period": 9,"sex": 0,"symptoms": "1001"}
the_data= {'day': 1, 'period': 1, 'goodsInfo': [{"goodsId": 1213,"goodsNumber":1}], 'sex': 0, 'symptoms': 1001}
#[27, 'post', '我方没有的商品', 1, '[{"goodsId": 651612313125131,"goodsNumber":1}}]', 9, 1, 1001, '"code": 1000', '"data"!=none']
Httpreq(the_url,the_data,'').req_1()
#print(asdf)