-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.py
39 lines (30 loc) · 1.01 KB
/
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
import socketserver
from http.server import BaseHTTPRequestHandler
from urllib.parse import urlparse
from primality import Primality_test
primality = Primality_test()
PORT = 80
class Request_handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
query = urlparse(self.path).query
query_components = dict(qc.split("=") for qc in query.split("&"))
candidate_number = query_components["number"]
if candidate_number:
prime = primality.isPrime(int(candidate_number))
if prime:
message = "Given number " + str(candidate_number) + " is prime."
else:
message = "Given number " + str(candidate_number) + " is NOT prime."
self.wfile.write(bytes(message, "utf8"))
return
return
class Server():
def __init__(self):
pass
def run(self):
with socketserver.TCPServer(("", PORT), Request_handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()