Created
December 1, 2023 16:38
-
-
Save BeautyyuYanli/a4ef33fe1325ed967bcf7bcbd18ad633 to your computer and use it in GitHub Desktop.
Listen for http request only once, to complete OAuth2.0 locally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from typing import Optional | |
def http_once( | |
host: str, | |
port: int, | |
buffer_size: int = 4096, | |
timeout: Optional[float] = None, | |
response: str = "OK", | |
) -> bytes: | |
s = socket.create_server((host, port)) | |
s.settimeout(timeout) | |
conn, _addr = s.accept() | |
with conn: | |
conn.settimeout(timeout) | |
request = conn.recv(buffer_size) | |
conn.sendall(response.encode("utf-8")) | |
s.shutdown(socket.SHUT_RD) | |
s.close() | |
return request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment