def do_request(request: Request, timeout: int = 1000, max_iterations: int = 10) -> Response: while max_iterations >= 0: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if request.scheme == "https": sock = ssl.wrap_socket(sock) sock.settimeout(timeout) max_iterations -= 1 sock.connect( (request.host, 80 if request.scheme == 'http' else 443)) if request.scheme == "https": sock.do_handshake() sock.sendall(bytes(request)) obtained_data = b'' while True: data = sock.recv(1024) if not data: break obtained_data += data sock.close() response = Response.parse(obtained_data) if 300 <= response.code < 400: request.set_url(response.location) else: return response raise errors.MaxDirectionsError()
def test_do_request(): http_client = HttpClient() request = Request() request.set_host('vk.com') response = http_client.do_request(request, 1000, 10) assert response.code == 200 assert response.protocol == 1.1
def test_request(): request = Request() expected_bytes_request: str = 'GET / HTTP/1.1\r\n' \ 'Host: None\r\n' \ 'Connection: close\r\n' \ 'Content-Length: 0\r\n\r\n' assert bytes(request).decode() == expected_bytes_request
def prepare_request(arguments) -> Request: request = Request() if arguments.url is not None: request.set_url(arguments.url) elif arguments.host is not None: request.set_host(arguments.host) else: raise AttributeError if arguments.method: request.set_method(arguments.method) if arguments.path: request.set_path(arguments.path) if arguments.scheme: request.set_scheme(arguments.scheme) if arguments.my_headers: headers = {} for header in arguments.my_headers: separator_ind = header.find(":") key = header[0:separator_ind] value = header[separator_ind + 1::].strip() headers[key] = value request.set_headers(headers) if arguments.reference: request.set_header('Reference', arguments.reference) if arguments.cookie: request.set_header('Cookie', arguments.cookie) if arguments.cookiefile: with open(arguments.cookiefile, "r") as file: request.set_header('Cookie', file.read()) if arguments.agent: request.set_header('User-Agent', arguments.agent) if arguments.data: request.set_body(arguments.data) if arguments.file: with open(arguments.file, 'r') as file: request.set_body(file.read()) return request
def test_methods_with_host(): request = Request() request.set_host('vk.com') request.set_path('/feed/ru') request.set_scheme('http') request.set_body('Hello') request.set_headers({'Cookie': '1234', 'Reference': 'blablacar.com'}) request.set_header(header='User-Agent', value='Yandex') request.set_method('delete') request.set_protocol('HTTP/1.0') expected_bytes_request: str = 'DELETE /feed/ru HTTP/1.0\r\n' \ 'Host: vk.com\r\n' \ 'Connection: close\r\n' \ 'Content-Length: 5\r\n' \ 'Cookie: 1234\r\n' \ 'Reference: blablacar.com\r\n' \ 'User-Agent: Yandex\r\n\r\n' \ 'Hello' assert bytes(request).decode() == expected_bytes_request assert request.scheme == 'http' assert request.host == 'vk.com'
def test_set_method_with_url(): request = Request() request.set_url('https://vk.com/feed') request.set_body('Hello') request.set_headers({'Cookie': '1234', 'Reference': 'blablacar.com'}) request.set_header(header='User-Agent', value='Yandex') request.set_method('post') expected_bytes_request: str = 'POST /feed HTTP/1.1\r\n' \ 'Host: vk.com\r\n' \ 'Connection: close\r\n' \ 'Content-Length: 5\r\n' \ 'Cookie: 1234\r\n' \ 'Reference: blablacar.com\r\n' \ 'User-Agent: Yandex\r\n\r\n' \ 'Hello' assert bytes(request).decode() == expected_bytes_request
def _execute(self, url, params=None, token=None, version=None): fy_request = Request() fy_request.set_host(self.host) fy_request.set_method('post') fy_request.set_protocol(constant.HTTPS) fy_request.set_content_type(constant.CONTENT_TYPE_JSON) fy_request.set_url(url) fy_request.set_cloud_token(token) fy_request.set_params(params) fy_request.format_params() if version: fy_request.set_api_ver(version) return super().execute(fy_request)