-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptoquote.py
executable file
·66 lines (55 loc) · 2.05 KB
/
cryptoquote.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
#!/usr/bin/python3
# from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from http.server import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep
import cgi
import random
import string
PORT_NUMBER = 8081
def good_input(input):
#Tests if valid input (No Numbers/special characters except ones strictly defined)
return not set(input).difference(string.ascii_letters + ' ' + '.' + ',' + '?' + '!' + '-' + "'" + '"')
def converter(input):
if good_input(input) == False:
return 'INVALID CRYPTOQUOTE SYNTAX... NO NUMBERS'
#Converts user input to crypto version
alphabet = string.ascii_lowercase
cypherbet = ''.join(random.sample(alphabet,len(alphabet)))
convert_table = str.maketrans(alphabet, cypherbet)
converted = input.translate(convert_table)
return(converted)
# This class will handle any incoming request from
# a browser
class myHandler(BaseHTTPRequestHandler):
# Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
f = open(curdir + sep + "cryptoquote.html")
self.wfile.write(f.read().encode())
f.close()
return
#Handler for the POST requests
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
original = form['cryptoquote'].value
self.send_response(200)
self.end_headers()
converted = converter(original)
self.wfile.write(converted.encode())
return
try:
# Create a web server and define the handler to manage the
# incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print ('Started httpserver on port',PORT_NUMBER)
# Wait forever for incoming http requests
server.serve_forever()
except KeyboardInterrupt:
print ('^C received, shutting down the web server')