-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebserver.py
More file actions
110 lines (83 loc) · 2.78 KB
/
webserver.py
File metadata and controls
110 lines (83 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
This program implements a webserver for receiving the OAuth 2.0 code
This is a single shot web server. Only one request is processed and then the
web server exits.
"""
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qsl
# The listening port can be anything but must be available
web_port = int(9000)
flag_continue = True
class ProcessRequests(BaseHTTPRequestHandler):
"""
Web server callback function to process GET, POST, etc.
"""
def log_message(self, format, *args):
"""
This function noops the log message for requests received.
"""
return
def do_GET(self):
"""
Process the web server GET request.
"""
rcvd_code = None
#
# process the query parameters. We are looking for a "code".
#
sys.stderr.write('Query: {}\n'.format(urlparse(self.path).query))
items = parse_qsl(urlparse(self.path).query)
for item in items:
if 'code' in item:
global flag_continue
sys.stderr.write('Found item: {} = {}\n'.format(item[0], item[1]))
sys.stdout.write(item[1])
rcvd_code = item[1]
flag_continue = False
if 'error' in item:
sys.stderr.write('Found item: {} = {}\n'.format(item[0], item[1]))
sys.stderr.write(item[1] + '\n')
if rcvd_code is None:
sys.stderr.write('Error: Invalid request: {}\n'.format(self.path))
sys.stderr.write('Error: Request does not include query param "code=value"')
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
return
# Notice that this url is 'https' which must be specified for the redirect
# FIX
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
html = b"""
"<html><head><meta http-equiv='refresh' content='10;url=https://google.com'></head>
<body>Please return to the app.</body></html>")
"""
self.wfile.write(html)
def run_local_webserver(server_class=HTTPServer, handler_class=ProcessRequests, port=web_port):
"""
This function implements a web server. Once the web browser calls this server
with a 'code', the server prints the code and exits.
"""
server_address = ('', port)
try:
httpd = server_class(server_address, handler_class)
except:
e_type = sys.exc_info()[0]
e_msg = sys.exc_info()[1]
sys.stderr.write('\n')
sys.stderr.write('****************************************\n')
sys.stderr.write('Error: Cannot start local web server on port {}\n'. format(web_port))
sys.stderr.write('Error: %s\n', e_type)
sys.stderr.write('Error: %s\n', e_msg)
exit(1)
# sys.stderr.write('Starting httpd...\n')
sys.stderr.write('Listening on port {} ...\n'.format(web_port))
while flag_continue:
httpd.handle_request()
# All done.
if __name__ == "__main__":
# print('Hello world from Python')
run_local_webserver()
exit(0)