import webbrowser from http.server import BaseHTTPRequestHandler, HTTPServer, SimpleHTTPRequestHandler import urllib.parse as urlparse from urllib.parse import parse_qs import sys import requests as r API_ADDRESS = "http://localhost:1285" PORT = 3000 class S(BaseHTTPRequestHandler): def _set_response(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): parsed = urlparse.urlparse(str(self.path)) ticket = str(parse_qs(parsed.query)['ticket'][0]) print(ticket) response = r.get(API_ADDRESS + "/token?ticket=" + ticket) self._set_response() self.wfile.write("Your token: {}".format( response.text).encode('utf-8')) def wait_for_request(server_class=HTTPServer, handler_class=S): server_address = ('localhost', PORT) httpd = server_class(server_address, handler_class) return httpd.handle_request() url = 'https://cas.amu.edu.pl/cas/login?service=http://localhost:' + \ str(PORT) + '&locale=pl' webbrowser.open_new_tab(url) wait_for_request()