Last active
April 2, 2021 18:53
-
-
Save EONRaider/d6041f6c5845f9b8e726bed33d09c6c6 to your computer and use it in GitHub Desktop.
Send a HTTP GET request using socket in Python 3
This file contains 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
#!/bin/env/python3 | |
# https://gist.github.com/EONRaider/d6041f6c5845f9b8e726bed33d09c6c6 | |
__author__ = 'EONRaider, keybase.io/eonraider' | |
from socket import socket | |
def get_request(hostname: str, path: str = None, port: int = 80, | |
encoding: str = 'utf_8'): | |
path = path if path is not None else '/' | |
with socket() as sock: | |
request = f'GET {path} HTTP/1.1\r\n' | |
f'host:{hostname}\r\n\r\n' | |
sock.connect((hostname, port)) | |
sock.send(request.encode(encoding)) | |
response = b''.join(chunk for chunk in | |
iter(lambda: sock.recv(24), b'')) | |
return response.decode(encoding) | |
if __name__ == '__main__': | |
response = get_request('target_host') | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment