Skip to content

Document integrating HTTPParser / NetworkBackend. #131

@lovelydinosaur

Description

@lovelydinosaur

In the parsers documentation, let's get to the point where we can demo how to wire up the NetworkBackend and HTTPParser together to create simple one-shot connection requests...

def request(method: str, url: str) -> tuple(int, bytes):
    b = httpx.NetworkBackend()
    u = httpx.URL(url)
    secure = {"https": True, "http": False}[u.scheme]

    with b.connect(u.host, u.port, secure=secure) as stream:
        p = httpx.HTTPParser(stream, mode='CLIENT')
        p.send_method_line(method, u.target, "HTTP/1.1")
        p.send_headers([("Host", u.netloc), ("Connection", "close")])
        p.send_body("")
        protocol, status, reason = p.recv_status_line()
        headers = p.recv_headers()
        s = httpx.HTTPStream(p)
        body = s.read()

    return status, body

status, body = request("GET", "https://www.example.com")

Some points still todo here...

  • Neaten up API. for .connect() on NetworkBackend. (eg. secure by default.)
  • HTTPStream to accept parser instance. Document this class in the Parser docs.
  • Review bytes/str above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions