-
Notifications
You must be signed in to change notification settings - Fork 6
/
numberserver.py
49 lines (39 loc) · 1.3 KB
/
numberserver.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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import socket
class NumberServer(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
def do_GET(self):
self._set_headers()
# Try and open number file and read number
try:
f = open("number.txt", "r")
number = int(f.read())
f.close()
except IOError, ValueError:
# if we fail then just assume 1
number = 1
# send the number to the client
self.wfile.write(str(number))
# write incremented number back to file
f = open("number.txt", "w")
number = str(number+1)
f.write(number)
f.close()
def do_HEAD(self):
self._set_headers()
class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6
def run(server_class=HTTPServerV6, handler_class=NumberServer, port=80):
server_address = ('::', port)
httpd = server_class(server_address, handler_class)
print 'Starting number server...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()