forked from jongracecox/anybadge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanybadge_server.py
executable file
·160 lines (115 loc) · 5.2 KB
/
anybadge_server.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
"""
Anybadge Server
This package provides a server for anybadge.
"""
from __future__ import print_function
from os import environ
import logging
import argparse
from anybadge import Badge
# Import the correct version of HTTP server
try:
from http.server import HTTPServer, BaseHTTPRequestHandler
except ImportError:
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
# Import the correct version of urlparse, depending on which version
# of Python we are using
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
logger = logging.getLogger(__name__)
DEFAULT_SERVER_PORT = 8000
DEFAULT_SERVER_LISTEN_ADDRESS = 'localhost'
DEFAULT_LOGGING_LEVEL = logging.INFO
SERVER_PORT = DEFAULT_SERVER_PORT
SERVER_LISTEN_ADDRESS = DEFAULT_SERVER_LISTEN_ADDRESS
class AnybadgeHTTPRequestHandler(BaseHTTPRequestHandler):
"""Request handler for Anybadge HTTP server."""
def do_HEAD(self):
logging.debug('Sending head.')
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
logging.debug('Handling get request.')
self.do_HEAD()
# Ignore request for favicon
if self.path == '/favicon.ico':
logging.debug('Ignoring favicon request.')
return
# Parse the URL query string
parsed = urlparse.urlparse(self.path)
url_query = urlparse.parse_qs(parsed.query)
label = ''
value = ''
color = 'green'
# Extract the label and value portions
if 'label' in url_query:
label = url_query['label'][0]
if 'value' in url_query:
value = url_query['value'][0]
logging.debug('Label: %s Value: %s', label, value)
if label and value and color:
logging.debug('All parameters present.')
badge = Badge(label=label, value=value, default_color=color)
for line in badge.badge_svg_text:
self.wfile.write(str.encode(line))
else:
logging.debug('Not all parameters present.')
self.wfile.write(b"<html><head><title>Anybadge Web Server.</title></head>")
self.wfile.write(b"<body>")
help_text = """
<h1>Welcome to the Anybadge Web Server.</h1>
You are seeing this message because you haven't passed all the query parameters
to display a badge.
You need to pass at least a <b>label</b> and <b>value</b> parameter.
Here is an example:
<a href="http://localhost:{port}/?label=Project%20Awesomeness&value=110%">\
http://localhost:{port}/?label=Project%20Awesomeness&value=110%</a>
""".format(port=SERVER_PORT)
for line in help_text.splitlines():
self.wfile.write(str.encode('<p>%s</p>' % line))
self.wfile.write(b"</body></html>")
def run(listen_address=DEFAULT_SERVER_LISTEN_ADDRESS, port=DEFAULT_SERVER_PORT):
server_address = (listen_address, port)
global SERVER_PORT, SERVER_LISTEN_ADDRESS
SERVER_PORT = port
SERVER_LISTEN_ADDRESS = listen_address
httpd = HTTPServer(server_address, AnybadgeHTTPRequestHandler)
logging.info('Serving at: http://%s:%s' % server_address)
httpd.serve_forever()
def parse_args():
logger.debug('Parsing command line arguments.')
parser = argparse.ArgumentParser(description="Run an anybadge server.")
parser.add_argument('-p', '--port', type=int, default=DEFAULT_SERVER_PORT,
help="Server port number. Default is %s. This can also be set via an environment "
"variable called ``ANYBADGE_PORT``." % DEFAULT_SERVER_PORT)
parser.add_argument('-l', '--listen-address', type=str, default=DEFAULT_SERVER_LISTEN_ADDRESS,
help="Server listen address. Default is %s. This can also be set via an environment "
"variable called ``ANYBADGE_LISTEN_ADDRESS``." % DEFAULT_SERVER_LISTEN_ADDRESS)
parser.add_argument('-d', '--debug', action='store_true', help='Enable debug logging.')
return parser.parse_args()
def main():
"""Run server."""
global DEFAULT_SERVER_PORT, DEFAULT_SERVER_LISTEN_ADDRESS, DEFAULT_LOGGING_LEVEL
# Check for environment variables
if 'ANYBADGE_PORT' in environ:
DEFAULT_SERVER_PORT = environ['ANYBADGE_PORT']
if 'ANYBADGE_LISTEN_ADDRESS' in environ:
DEFAULT_SERVER_LISTEN_ADDRESS = environ['ANYBADGE_LISTEN_ADDRESS']
if 'ANYBADGE_LOG_LEVEL' in environ:
DEFAULT_LOGGING_LEVEL = logging.getLevelName(environ['ANYBADGE_LOG_LEVEL'])
# Parse command line args
args = parse_args()
# Set logging level
logging_level = DEFAULT_LOGGING_LEVEL
if args.debug:
logging_level = logging.DEBUG
logging.basicConfig(format='%(asctime)-15s %(levelname)s:%(filename)s(%(lineno)d):%(funcName)s: %(message)s',
level=logging_level)
logger.info('Starting up anybadge server.')
run(listen_address=args.listen_address, port=args.port)
if __name__ == '__main__':
main()