• python自动化之requests库封装


    import requests

    # 一次实例化多次调用
    class HTTPRequest():
    cookie = None
    def do_request(self,method, url, data =None,headers= None):
    method = method.upper()
    resp = None
    if method == "POST":
    resp = requests.post(url=url, data=data, headers=headers, cookies= self.cookie)
    elif method == "GET":
    resp = requests.get(url=url, params=data, headers=headers, cookies = self.cookie)
    else:
    print("不支持该请求方式:{}".format(method))
    if resp.cookies: # 判断是否有cookies,有就存放在类属性里面
    self.cookie = resp.cookies
    print(self.cookie)
    return resp


    if __name__ == '__main__':
       # 可将前一个接口填写有cookieurl(登录),看是否会传给下一个接口

    method = "get"
    url = "填入自己的请求url"
    data = {"mobilephone": "15873731233", "pwd": "123456"}
    request = HTTPRequest()
    respon= request.do_request(method,url,data)
    print(respon.text)

    method = "post"
    url = "填入自己的请求url"
    data = {"mobilephone":"15810447878","amount":"100"}
    respon = request.do_request(method, url, data)
    print(respon.text)



  • 相关阅读:
    java通过dom读写xml文件
    SWT
    Eclipse RCP学习资料
    Integration Guide
    一个Linux命令,黑客帝国
    GET和POST两种基本请求方法的区别
    贫血模式-AnemicDomainModel
    Synchronized锁在Spring事务管理下,为啥还线程不安全?
    redis 集群配置、启动
    redis cluster集群
  • 原文地址:https://www.cnblogs.com/xintiaoab/p/13427359.html
Copyright © 2020-2023  润新知