-
Notifications
You must be signed in to change notification settings - Fork 0
Document integrating HTTPParser / NetworkBackend. #131
Copy link
Copy link
Open
Description
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()onNetworkBackend. (eg. secure by default.) HTTPStreamto accept parser instance. Document this class in the Parser docs.- Review bytes/str above.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels