-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
84 lines (76 loc) · 3.27 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
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
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import translate
# This class is used to handle http requests
class S(BaseHTTPRequestHandler):
# This method is used to send response header to client
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
# This method is triggered when GET request is sent to server
def do_GET(self):
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n",
str(self.path), str(self.headers))
self._set_response()
pageData = str.encode("404 NOT FOUND")
if str(self.path) == "/" or str(self.path) == "/index.html":
f = open("WebData/firstPage.htm", "rb")
pageHtml = (f.read()).decode()
pageBytes = formatMainPage(pageHtml)
f.close()
self.wfile.write(pageBytes)
# This method is triggered when POST request is sent to server
def do_POST(self):
# <--- Gets the size of data
content_length = int(self.headers['Content-Length'])
# <--- Gets the data itself
post_data = self.rfile.read(content_length)
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
str(self.path), str(self.headers), post_data.decode('utf-8'))
pageData = str.encode("INCORRECT POST DATA")
if len(post_data) >= 22 and str(self.path) == "/translate":
pageData = formatResultPage(post_data)
self._set_response()
self.wfile.write(pageData)
# This function is used to format and display main webpage
def formatMainPage(html):
return (html.replace("<REPLACE_WITH_LANGUAGES>", translate.getSupportedLanguagesForSelect())).encode()
# This function is used to format and display result webpage
def formatResultPage(post_data):
splitData = post_data.decode().split('\r\n')
language = splitData[0].split('=')[1]
text = splitData[1].split('=')[1]
text = text.replace("\r", "")
translated_google, translated_deepl = translate.translate(language, text)
if language[:3] == 'gt:': language = language[3:]
f = open("WebData/translatePage.htm", "rb")
pageHtml = (f.read()).decode()
pageHtml = pageHtml.replace("{ORIGINAL_TEXT}", text)
pageHtml = pageHtml.replace(
"{TRANSLATED_TEXT_GOOGLE}", translated_google)
pageHtml = pageHtml.replace(
"{TRANSLATED_TEXT_DEEPL}", translated_deepl)
pageHtml = pageHtml.replace("{SPECIFY_LANGUAGE}", language)
pageData = pageHtml.encode('utf-8')
f.close()
return pageData
# This function is used to run HTTPServer with specified options
def run(server_class=HTTPServer, handler_class=S, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f"Starting ar http://localhost:{port}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...\n')
# Program starts here
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()