Last active
November 11, 2022 22:03
-
-
Save mikf/c069fc6173d37a9365ec7d3f33c0594d to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
"""Get User-Agent header from default browser""" | |
import webbrowser | |
import socket | |
def user_agent(): | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
server.bind(("127.0.0.1", 6414)) | |
server.listen(1) | |
webbrowser.open("http://127.0.0.1:6414/user-agent") | |
client = server.accept()[0] | |
server.close() | |
for line in client.recv(1024).split(b"\n"): | |
key, _, value = line.partition(b":") | |
if key.strip().lower() == b"user-agent": | |
useragent = value.strip() | |
break | |
else: | |
useragent = b"" | |
client.send(b"HTTP/1.1 200 OK\r\n\r\n" + useragent) | |
client.close() | |
return useragent.decode() | |
if __name__ == "__main__": | |
print(user_agent()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment