Created
August 20, 2019 04:46
-
-
Save qstrahl/16cf6373bc563a9b01add85ead1ece0f to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import webbrowser | |
from webbrowser import WindowsDefault, BackgroundBrowser | |
from urllib.parse import parse_qs, quote | |
chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" | |
safe = "%/:=&?~#+!$,;'@()*[]" | |
class ChromeBrowser(BackgroundBrowser): | |
def open(self, url, new=0, autoraise=True): | |
url = url if quote(url, safe=safe) == url else "? " + url | |
return super().open(url, new=new, autoraise=autoraise) | |
def application(environ, start_response): | |
try: | |
if environ['REQUEST_METHOD'] == 'POST': | |
try: | |
length = int(environ['CONTENT_LENGTH']) | |
except KeyError: | |
length = None | |
post = parse_qs(environ['wsgi.input'].read(length).decode('utf-8')) | |
[url] = post['url'] | |
webbrowser.open(url, new=2, autoraise=True) | |
except Exception as e: | |
print(repr(e)) | |
start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')]) | |
return [] | |
if __name__ == '__main__': | |
from wsgiref.simple_server import make_server | |
webbrowser.register("chrome", ChromeBrowser, ChromeBrowser(chrome), preferred=True) | |
webbrowser.register("windows-default", WindowsDefault, preferred=True) | |
httpd = make_server('127.0.0.1', 1337, application) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment