码迷,mamicode.com
首页 > 其他好文 > 详细

py requests.post2

时间:2018-06-28 17:37:23      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:gen   gzip   dict   \n   window   like   cep   ext   foo   

# -*- coding: utf-8 -*-
import json
import requests



headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11‘,
‘Accept‘: ‘text/html;q=0.9,*/*;q=0.8‘,
‘Accept-Charset‘: ‘ISO-8859-1,utf-8;q=0.7,*;q=0.3‘,
‘Accept-Encoding‘: ‘gzip‘,
‘Connection‘: ‘close‘,
‘Referer‘: None
}

# 自定义类
class MyClass:
# 初始化
def __init__(self):
print("__init__=====>self: " , "self")

# 创建MyClass对象
myClass = MyClass()
# 对象转化为字典
myClassDict = myClass.__dict__
# 字典转化为json
myClassJson = json.dumps(myClassDict,sort_keys=True, indent=2,ensure_ascii=False)
myClassLoads = json.loads(myClassJson)


def reqSend(obj, url,log):
try:
myClassDict = obj.__dict__
myClassJson = json.dumps(myClassDict, sort_keys=True, indent=2, ensure_ascii=False)
myClassLoads = json.loads(myClassJson)
r1 = requests.post(url, data=myClassLoads, headers=headers)
r1.encoding = ‘utf-8‘
log.warn("***执行接口>>>>>"+url+‘\n【请求参数】\n‘+myClassJson)
r1Text = json.dumps(json.loads(r1.text),sort_keys=True, indent=2,ensure_ascii=False)
log.debug(‘【响应】\n‘+r1Text)
return r1Text;
except:
print("reqSend=====>except: " , "except")
pass

py requests.post2

标签:gen   gzip   dict   \n   window   like   cep   ext   foo   

原文地址:https://www.cnblogs.com/rhxuza1993/p/9239634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!