python requests 请求的封装

#encoding=utf-8
import requests
import json
class HttpClient(object):
    def __init__(self):
        pass

def __post(self,url,data=None,json=None,**kargs):
        response=requests.post(url=url,data=data,json=json)
        return response

def __get(self,url,params=None,**kargs):
        response=requests.get(url=url,params=params)

def request(self,requestMethod,requestUrl,paramsType,requestData=None,headers=None,cookies=None):
        if requestMethod.lower() == "post":
            if paramsType == "form":
                response=self.__post(url=requestUrl,data=json.dumps(eval(requestData)),headers=headers,cookies=cookies)
                return response
            elif paramsType == ‘json‘:
                response = self.__post(url=requestUrl,json=json.dumps(eval(requestData)),headers=headers,cookies=cookies)
                return response
        elif requestMethod == "get":
            if paramsType == "url":
                request_url="%s%s" %(requestUrl,requestData)
                response=self.__get(url=request_url,headers=headers,cookies=cookies)
                return response
            elif paramsType == "params":
                response=self.__get(url=requestUrl,params=requestData,headers=headers,cookies=cookies)
                return response

if __name__ == "__main__":
    hc=HttpClient()
    response=hc.request("post","http://39.106.41.11:8080/register/","form",‘{"username":"xufengchai6","password":"xufengchai121","email":"[email protected]"}‘)
    print response.text

结果:

C:\Python27\python.exe D:/test/interfaceFramework_practice1/util/httpClient.py
{"username": "xufengchai6", "code": "01"}

Process finished with exit code 0

原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9426071.html

时间: 2024-07-31 20:14:02

python requests 请求的封装的相关文章

python requests请求卡住问题

最近经常接到别人反馈某个爬虫工具程序没有正常运行,需要下载的资讯数据也没有及时进行收录. 刚开始以为可能是机器的问题,偶尔机器会出现程序运行中途卡住的情况. 但随着异常的情况越来越频繁,我便只好去排查问题. 通过查看程序运行的日志信息,发现程序总是卡在requests请求的那一步. 这让我觉得很奇怪,这一步为什么会卡住呢,我已经设置了超时时间,按理说超时的话程序会进入异常啊,然而却什么异常信息都没有输出. 查找了相关的资料,发现确实存在这种情况. requests文档上有写,timeout is

python requests请求指定IP的域名

不需要修改/etc/hosts,curl直接解析ip请求域名 以上是curl版本的,现在增加一个python版本的 import requests requests.get('https://192.168.100.4',headers={'Host':'test.com'},verify=False) or import urllib2 req=urllib2.Request('https://192.168.199.4',headers={'Host':'test.com'})urllib2

大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url, **kwargs)-->session.request(method="get", url=url, **kwargs)-->session.send(request, **kwargs)-->adapter.send(request, **kwargs)-->

python requests函数封装方法

python  requests函数封装方法 上代码 1 import requests 2 import json 3 4 """ 5 封装request请求, 6 1.post:my_post 7 2.get:my_get 8 3.返回code:get_code(res) 9 4.返回json:get_json(res) 10 5.返回text:get_text(res) 11 6.响应时间:get_time(res) 12 7.请求header:get_header(a

python代理池的构建1——代理IP类的构建,以及配置文件、日志文件、requests请求头

一.整体结构 二.代理IP类的构建(domain.py文件) ''' 实现_ init_ 方法, 负责初始化,包含如下字段: ip: 代理的IP地址 port:代理IP的端口号 protocol: 代理IP支持的协议类型,http是0, https是1, https和http都支持是2 nick_ type: 代理IP的匿名程度,高匿:0,匿名: 1,透明:2 speed:代理IP的响应速度,单位s area:代理IP所在地区 score:代理IP的评分,用于衡量代理的可用性;默认分值可以通过配

python+requests——发送post请求——各种情况

requests库是 python3 中非常优秀的第三方库,它使用 Apache2 Licensed 许可证的 HTTP 库,用 Python 编写,真正的为人类着想. requests 使用的是 urllib3(python3.x中的urllib),因此继承了它的所有特性. Requests 会自动实现持久连接keep-alive,Requests 支持 HTTP 连接保持和连接池,支持使用 cookie 保持会话,支持文件上传, 支持自动确定响应内容的编码,支持国际化的 URL 和 POST

python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 运行完后,重新生成一个excel报告,结果写入excel 用unittest+ddt数据驱动模式执行 HTMLTestRunner生成可视化的html报告 对于没有关联的单个接口请求是可以批量执行的,需要登录的话写到setUpclass里的session里保持cookies token关联的

Python Requests 小技巧总结

关于 Python Requests ,在使用中,总结了一些小技巧把,分享下. 1:保持请求之间的Cookies,我们可以这样做. import requests self.session = requests.Session() self.session.get(login_url) # 可以保持登录态 2:请求时,会加上headers,一般我们会写成这样 self.session.get(url, params, headers=headers) 唯一不便的是之后的代码每次都需要这么写,代码

基于Python Requests的数据驱动的HTTP接口测试

发表于:2017-8-30 11:56  作者:顾翔   来源:51Testing软件测试网原创 http://www.51testing.com/html/69/n-3720769-2.html 1.测试金字塔 图 1软件测试金字塔 图 1是Main Cohn提出的软件测试金字塔,他认为作为一个测试工程师应该把大量的工作花在单元测试和接口测试,而其余的发在UI测试以及探索式测试.纵然,单元测试的优点很突出,它接近于代码本身,运行速度快,开发可以一边写产品代码一边写单元测试代码,一旦在单元测试中