コード例 #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_request_init_medium(self):
     Request(
         method="GET",
         target="google.com:80",
         protocol="HTTP/1.1",
         headers=None,
         body=None,
     )
コード例 #3
0
def server():
    """Simple socket server that uses httpsuite to interpret and reply.

    1. Opens a new socket.
    2. Binds to 127.0.0.1:8080 and waits until new connection.
    3. Accepts connection from external source.
    4. Receive the data from the client.
    5. Parse the clients request.
    6. Interpret the request.
    7. Reply to the client.
    8. Close the connection with the client.
    """

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

    # 2. Binds to 127.0.0.1:8080 and waits until new connection.
    s.bind(("127.0.0.1", 8080))
    s.listen(1)

    # 3. Accepts connection from external source.
    conn, address = s.accept()

    print("===== Connecting With New Client =====", "\n")
    print(address, "\n")

    # 4. Receive the data from the client.
    data = conn.recv(1024)

    # 5. Parse the clients request.
    request = Request.parse(data)

    print("===== Received Data From Client =====", "\n")
    print(request, "\n")

    # 6. Interpret the request.
    response = Response(protocol="HTTP/1.1", status=200, status_msg="OK")
    if request.target == "/":
        response.body = "Homepage of the microservice."
    elif request.target == "/data":
        response.body = "You are accessing the /data directory of this microservice."
    else:
        response.status = 404
        response.status_msg = "Not Found"

    print("===== Replying to Client =====", "\n")
    print(response, "\n")

    # 7. Reply to the client.
    conn.sendall(response.raw)

    print("===== Closing Connection to Client =====", "\n")

    # 8. Close the connection with the client, and the server.
    conn.close()
    s.close()
コード例 #4
0
 def test_request_init_large(self):
     Request(
         method="GET",
         target="/",
         protocol="HTTP/1.1",
         headers=Headers({
             "Host": "www.google.com",
             "Accept": "*/*",
             "User-Agent": "httpsuite/1.0.0",
             "Connection": "keep-alive",
         }),
         body=json.dumps({"hello": "world"}),
     )
コード例 #5
0
    def test_request_str(self):
        parsed = Request.parse(request_raw)
        assert isinstance(parsed, Request)

        assert parsed.method == "POST"
        assert parsed.target == "/"
        assert parsed.protocol == "HTTP/1.1"
        assert parsed.headers == Headers({
            "Host": "www.google.com",
            "Accept": "*/*",
            "User-Agent": "httpsuite/1.0.0",
            "Connection": "keep-alive",
        })
        assert parsed.body == '{"hello": "world"}'
コード例 #6
0
 def test_request_init_small(self):
     Request(
         method="GET",
         target="google.com:80",
         protocol="HTTP/1.1",
     )
コード例 #7
0
 def test_init_wrong_type(self):
     with pytest.raises(TypeError):
         Request(method={}, target={}, protocol={})
コード例 #8
0
from httpsuite import Request, Response
import socket
import json

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

# 2. Creates the request to be sent via Request object.
body = json.dumps({"hello": "world"})
request = Request(
    method="POST",
    target="/post",
    protocol="HTTP/1.1",
    headers={
        "Host": "httpbin.org",
        "Connection": "close",
        "Content-Length": len(body),
        "Accept": "*/*",
    },
    body=body,
)

# Prints the raw request.
print("====== Raw Request ======", "\n")
print(request.raw, "\n")

# 3. Connect to httpbin.org via socket.
s.connect(("httpbin.org", 80))

# 4. Send generated request to server.
s.sendall(request.raw)
コード例 #9
0

# ====================
#       Request
# ====================
request_headers = {
    "Host": "www.google.com",
    "Accept": "*/*",
    "User-Agent": "httpsuite/1.0.0",
    "Connection": "keep-alive",
}
request_body = json.dumps({"hello": "world"})
request = Request(
    method="GET",
    target="/",
    protocol="HTTP/1.1",
    headers=Headers(request_headers),
    body=request_body,
)
request_raw = b'GET / HTTP/1.1\r\nHost: www.google.com\r\nAccept: */*\r\nUser-Agent: httpsuite/1.0.0\r\nConnection: keep-alive\r\n\r\n{"hello": "world"}'
request_first_line = b"GET / HTTP/1.1"

# ====================
#       Response
# ====================
response_headers = {
    "Host": "www.google.com",
    "Accept": "*/*",
    "Connection": "keep-alive",
    "Keep-Alive": "timeout=5, max=1000",
    "Server": "httpsuite/1.0.0",