【pytest】接口自动化:Requests如何设置代理,访问内网接口requests.exceptions.ConnectTimeout: HTTPSConnectionPool

引言:网络有代理时,使用requests.get()等请求,会提示超时。在请求中加入代理设置,则可以正常使用。

一、问题截图如下:

E                   requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='xxxx.com', port=443): Max retries exceeded with url: /usersLogin/login (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001E3772791E0>, 'Connection to 10.37.84.94 timed out. (connect timeout=None)'))
 

二、原因:

我是在公司内网环境访问外网域名,在连接的时候报错,无法连接,超时;后来定位到是由于公司的网络拦截导致,需要设置代理

三、Requests设置代理:

class TestLogin(CommonUtil):

   
    @pytest.mark.parametrize('caseinfo',read_yaml_testcase('testcase/test_login.yaml'))
    def test_login_01(self,caseinfo):
        print("\n测试01号登录成功")
        print(caseinfo)
        name = caseinfo['name']
        method = caseinfo['request']['method']
        url =  caseinfo['request']['url']
        data = caseinfo['request']['data']
        header= caseinfo['request']['headers']
        validate = caseinfo['validate']

        proxy = {'http':'http://username:password@ip:port',
                 'https': 'https://username:password@ip:port'
                 }



        res = RequestsUtil().send_all_request(method= method, url=url,headers=header, data=data,proxies=proxy)

1、设置http和https代理,根据接口是http请求或者是https请求,进行对应的设置(也可以两个都写上)

2、ip:port,代理服务器的ip和端口,由代理服务器来将请求发送给目标服务器

3、username:password:代理网络的用户名和密码,有些需要填,有些不需要填,如果不知道的话,可以先不填试试

# 1:普通的代理

proxies = {"http":"http://12.34.56.79:9527",
           "https":"https://12.34.56.79:9527"}
res = requests.get(url="http://www.baidu.com",proxies=proxies)
print(res.content.decode("utf-8"))


# 2:携带登录的用户名和密码
proxies1 = {"http":"http://用户名:密码@12.34.56.79:9527",
            "https":"https://用户名:密码@12.34.56.79:9527"
             }
res = requests.get(url="http://baidu.com",proxies=proxies1)
print(res.content.decode("utf-8"))

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的接口自动化框架: 1. 安装依赖库 ``` pip install pytest pip install requests ``` 2. 创建项目目录结构 ``` project ├── api │ ├── __init__.py │ └── user_api.py ├── config │ └── config.ini ├── testcases │ ├── __init__.py │ └── test_user_api.py ├── conftest.py └── pytest.ini ``` 3. 编写配置文件 config/config.ini ``` [host] url = http://localhost:8080/api/ ``` 4. 编写 API 接口封装 api/user_api.py ```python import requests import json from configparser import ConfigParser class UserApi: def __init__(self): self.config = ConfigParser() self.config.read('config/config.ini') self.url = self.config.get('host', 'url') def get_user(self, user_id): url = self.url + f'user/{user_id}' response = requests.get(url) return json.loads(response.text) def add_user(self, data): url = self.url + 'user' headers = {'Content-Type': 'application/json'} response = requests.post(url, data=json.dumps(data), headers=headers) return json.loads(response.text) def update_user(self, user_id, data): url = self.url + f'user/{user_id}' headers = {'Content-Type': 'application/json'} response = requests.put(url, data=json.dumps(data), headers=headers) return json.loads(response.text) def delete_user(self, user_id): url = self.url + f'user/{user_id}' response = requests.delete(url) return json.loads(response.text) ``` 5. 编写测试用例 testcases/test_user_api.py ```python from api.user_api import UserApi class TestUserApi: def setup_class(self): self.api = UserApi() def test_get_user(self): user_id = 1 user = self.api.get_user(user_id) assert user['id'] == user_id def test_add_user(self): data = {'name': 'test', 'age': 20} user = self.api.add_user(data) assert user['name'] == data['name'] def test_update_user(self): user_id = 1 data = {'name': 'test1'} user = self.api.update_user(user_id, data) assert user['name'] == data['name'] def test_delete_user(self): user_id = 1 user = self.api.delete_user(user_id) assert user['id'] == user_id ``` 6. 运行测试 在项目根目录下运行以下命令: ``` pytest ``` 运行结果如下: ``` ============================= test session starts ============================== platform win32 -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 rootdir: D:\project plugins: Faker-8.13.0 collected 4 items testcases\test_user_api.py .... [100%] ============================== 4 passed in 0.48s ============================== ``` 以上就是一个简单的接口自动化框架,你可以根据实际情况进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值