예제 #1
0
def client():
    """Simple socket client that uses httpmodule to request server resource.

    1. Opens a new socket.
    2. Connects the server.
    3. Creates a valid request to send to the server.
    4. Sends the request.
    5. Receives reply from the server.
    6. Parses the reply from the server.
    7. Closes connection with the server.
    """

    # Note: Sleeps so that the socket server can boot-up before.
    time.sleep(1)

    # 1. Opens a new socket.
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 2. Connects the server.
    s.connect(("127.0.0.1", 8080))

    # 3. Creates a valid request to send to the server.
    request = Request(method="GET", target="/", protocol="HTTP/1.1")

    # 4. Sends the request.
    s.sendall(request.raw)

    # 5. Receives reply from the server.
    data = s.recv(1024)

    # 6. Parses the reply from the server.
    response = Response.parse(data)

    # 7. Closes connection with the server.
    s.close()
예제 #2
0
    def test_response_str(self):
        parsed = Response.parse(response_raw)
        assert isinstance(parsed, Response)

        assert parsed.protocol == "HTTP/1.1"
        assert parsed.status == 200
        assert parsed.status_msg == "OK"
        assert parsed.headers == Headers({
            "Test": "Headers",
            "Host": "www.google.com",
            "Accept": "*/*",
            "Connection": "keep-alive",
        })
        assert parsed.body == '{"hello": "world"}'
예제 #3
0
# 3. Connect to httpbin.org via socket.
s.connect(("httpbin.org", 80))

# 4. Send generated request to server.
s.sendall(request.raw)

# 5. Receive raw response from server.
response_raw = s.recv(4096)

# Prints the raw response.
print("====== Raw Response ======", "\n")
print(response_raw, "\n")

# 6. Parse the server's response with Response object.
response = Response.parse(response_raw)

# Prints the request and the response (pretty-print).
print("====== Request and Response ======", "\n")
print(request, "\n")
print(response, "\n")

# 7. Loads the response's body via JSON.
body = json.loads(response.body.string)

# Prints the loaded json ('dumps' for pretty-print).
print("====== Json ======", "\n")
print(json.dumps(body, indent=4))

# 8. Close socket.
s.close()
예제 #4
0
import json

from httpsuite import Response, ENCODE

# 1. Creates the body of the request.
body = json.dumps({"hello": "world"})

# 2. Creates an HTTP response.
response = Response(
    protocol="HTTP/1.1",
    status=200,
    status_msg="OK",
    headers={
        "Host": "www.google.com",
        "Connection": "keep-alive",
        "Content-Length": len(body),
    },
    body=body,
)

# 3. Parses the equivalent response as the above.
response_parsed = Response.parse((b"HTTP/1.1 200 OK\r\n"
                                  b"Host: www.google.com\r\n"
                                  b"Connection: keep-alive\r\n"
                                  b"Content-Length: %i\r\n"
                                  b"\r\n"
                                  b"%b") % (len(body), body.encode(ENCODE)))