From b4ab6a47d98a51d6266aef5eda6d7fbf069b3edd Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 3 Jun 2024 09:00:28 +0400 Subject: [PATCH] Replace urllib with urllib3 --- main.py | 29 ++++++++++++++++++++--------- requirements.txt | 1 + 2 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 requirements.txt diff --git a/main.py b/main.py index 0cc41c2..f726448 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import socket -from urllib.request import urlopen, Request +import urllib3 +import logging import unittest from unittest import TestCase @@ -54,26 +55,36 @@ class TestUDP(TestCase): class TestHTTP(TestCase): + def setUp(self) -> None: + self.http = urllib3.PoolManager() + def test_get(self) -> None: - self._send_and_check(Request( + self._check_status(self.http.request( + 'GET', HTTP_URL + '/get?' + device.http_req_data(), )) def test_post(self) -> None: - self._send_and_check(Request( + self._check_status(self.http.request( + 'POST', HTTP_URL + '/post', - data=device.http_req_data().encode(), + body=device.http_req_data(), )) def test_json(self) -> None: - self._send_and_check(Request( + self._check_status(self.http.request( + 'POST', HTTP_URL + '/json', - data=device.json_req_data().encode(), + body=device.json_req_data(), )) - def _send_and_check(self, req: Request) -> None: - with urlopen(req) as resp: - self.assertEqual(resp.status, 200) + def _check_status(self, resp: urllib3.HTTPResponse) -> None: + logging.debug(resp.headers.items()) + logging.debug(resp.data) + self.assertEqual(resp.status, 200) + + def tearDown(self) -> None: + self.http.clear() if __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a42590b --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +urllib3