Created
October 1, 2019 16:24
-
-
Save MayankFawkes/ac9b4390985cbc80f6c261e8af8675a0 to your computer and use it in GitHub Desktop.
Simple Get Request
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
import socket | |
request_text = b'GET http://ip-api.com/json/ HTTP/1.1\r\nHost: ip-api.com\r\nUser-Agent: curl/7.55.1\r\nAccept: */*\r\nConnection: close\r\n\r\n' | |
def geocode(): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(('ip-api.com', 80)) | |
sock.sendall(request_text) | |
raw_reply = b'' | |
#print(sock.getsockname()) | |
while True: | |
more = sock.recv(2048) | |
if not more:break | |
raw_reply += more | |
headers = raw_reply.split(b'\r\n\r\n')[0] | |
data = raw_reply[len(headers)+4:] | |
print(headers.decode()) | |
print(data.decode("ascii")) | |
#print(raw_reply.decode("ascii")) | |
if __name__ == '__main__': | |
geocode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment