File: util.py

package info (click to toggle)
pytest-httpbin 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: python: 312; sh: 18; makefile: 14
file content (29 lines) | stat: -rw-r--r-- 641 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import socket


def get_raw_http_response(host, port, path):
    CRLF = b"\r\n"

    request = [
        b"GET " + path.encode("ascii") + b" HTTP/1.1",
        b"Host: " + host.encode("ascii"),
        b"Connection: Close",
        b"",
        b"",
    ]

    # Connect to the server
    with socket.socket() as s:
        s.connect((host, port))

        # Send an HTTP request
        s.send(CRLF.join(request))

        # Get the response (in several parts, if necessary)
        response = b""
        buffer = s.recv(4096)
        while buffer:
            response += buffer
            buffer = s.recv(4096)

        return response