#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":"xufengchai@qq.com"}')
    print response.text

结果:

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

Process finished with exit code 0

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

  1. python+requests 请求响应文本出错返回“登录超时”

    Python+requests请求响应:"msg":"登录过时" 1.出错原代码: import requests import json#页面按条件搜索返回相 ...

  2. python requests请求卡住问题

    最近经常接到别人反馈某个爬虫工具程序没有正常运行,需要下载的资讯数据也没有及时进行收录. 刚开始以为可能是机器的问题,偶尔机器会出现程序运行中途卡住的情况. 但随着异常的情况越来越频繁,我便只好去排查 ...

  3. python requests 请求禁用SSL警告信息解决

    Python3 requests模块发送HTTPS请求,关闭SSL 验证,控制台会输出以下错误: InsecureRequestWarning: Unverified HTTPS request is ...

  4. 基于Python+Requests+Pytest+YAML+Allure实现接口自动化

    本项目实现接口自动化的技术选型:Python+Requests+Pytest+YAML+Allure ,主要是针对之前开发的一个接口项目来进行学习,通过 Python+Requests 来发送和处理H ...

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

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

  6. python 网络请求类库 requests 使用

    python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 githubhttps://github.com/kennethreitz/req ...

  7. python requests函数封装方法

    python  requests函数封装方法 上代码 import requests import json """ 封装request请求, 1.post:my_pos ...

  8. python网页请求urllib2模块简单封装代码

    这篇文章主要分享一个python网页请求模块urllib2模块的简单封装代码. 原文转自:http://www.jbxue.com/article/16585.html 对python网页请求模块ur ...

  9. python mysql redis mongodb selneium requests二次封装为什么大都是使用类的原因,一点见解

    1.python mysql  redis mongodb selneium requests举得这5个库里面的主要被用户使用的东西全都是面向对象的,包括requests.get函数是里面每次都是实例 ...

随机推荐

  1. python3安装builtwith

    >>> import builtwith Traceback (most recent call last): File , in <module> File excep ...

  2. JQuery EasyUI 日期控件 怎样做到只显示年月,而不显示日

    标题问题的答案在OSChina中 http://www.oschina.net/question/2282354_224401?fromerr=lHJTcN89 我还是把这个记录下来 ======== ...

  3. POJ2387-Till the cows come home【最短路】

    A - Til the Cows Come Home POJ - 2387 Bessie is out in the field and wants to get back to the barn t ...

  4. HDU 4734 - F(x) - [数位DP][memset优化]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 Time Limit: 1000/500 MS (Java/Others) Memory Lim ...

  5. CodeForces 551C - GukiZ hates Boxes - [二分+贪心]

    题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...

  6. chrome Sources选项卡 设置JavaScript事件断点

    chrome console 可以查看执行的javascript么? 举个例子: 比如这张图片,163邮箱,我点击上一封邮件和下一封邮件执行的是javascript方法. 虽然在chrome的cons ...

  7. echarts 数据统计报表

    官网   http://echarts.baidu.com/index.html 我们下载好开发包后就可以开始了,第一步引入开发包,和需要的主题文件(可定义自己的主体文件),并定义好页面布局.2.0以 ...

  8. Mybatis插入数据后返回主键id

    有时候使用mybatis插入数据后,需要用到记录在数据库中的自增id,可以利用keyProperty来返回,赋值给实体类中的指定字段. 单条记录插入并返回 First, if your databas ...

  9. 用Monitor简单3步监控中间件ActiveMQ

    Apache ActiveMQ是一个基于JMX规范的纯Java消息中间件,它为应用系统提供高效.灵活的消息同步与异步传输处理.存储转发.可靠传输的特性. 消息队列对于应用的健康运行非常重要,作为运维人 ...

  10. 关于微信小程序,你想知道的他们都问了

    微信公开课深圳站小程序专场刚刚结束,大家通过"微信公开课+"互动小程序提出了许多问题.我们筛选了后台问得最多的九个问题进行解答,快来看看这里有没有你想要的答案吧! @谢杨:小程序是 ...