You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
593 B
Python

7 years ago
"""
Here we have turned was was a synchronous call into an async call with the use of the select module.
select is old however and OSs have newer system calls. Asyncio abstracts the system calls out, and
gives us primitives to build better async code.
"""
import select
import socket
s = socket.create_connection(("httpbin.org", 80))
s.send(b"GET /delay/5 HTTP/1.1\r\nHost: httpbin.org\r\n\r\n")
s.setblocking(False)
while True:
ready_to_read, ready_to_write, in_error = select.select([s], [], [])
if s in ready_to_read:
buf = s.recv(1024)
print(buf)
break