-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftca.py
More file actions
64 lines (54 loc) · 2.38 KB
/
ftca.py
File metadata and controls
64 lines (54 loc) · 2.38 KB
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
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import html
import base64
import os
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
parsed_path = urlparse(self.path)
query_params = parse_qs(parsed_path.query)
text_from_query = ''
if 's_b64' in query_params:
try:
base64_string = query_params['s_b64'][0]
base64_bytes = base64_string.encode('utf-8')
text_bytes = base64.b64decode(base64_bytes)
text_from_query = text_bytes.decode('utf-8')
except Exception as e:
print(f"Base64 decoding error: {e}")
text_from_query = "Text decoding error."
elif 's' in query_params:
text_from_query = query_params.get('s', [''])[0]
is_multiline = query_params.get('multiline', ['false'])[0].lower() == 'true'
default_rows = '4'
rows_value = default_rows
if 'rows' in query_params:
try:
int(query_params['rows'][0])
rows_value = query_params['rows'][0]
except (ValueError, IndexError):
print(f"Invalid 'rows' parameter received. Using default value: {default_rows}")
try:
with open('index.html', 'r', encoding='utf-8') as file:
html_content = file.read()
except FileNotFoundError:
self.send_response(404)
self.end_headers()
self.wfile.write(b"Error: index.html not found.")
print(f"Error: index.html not found in current directory: {os.getcwd()}")
return
if is_multiline:
safe_text = html.escape(text_from_query)
html_content = html_content.replace('{TEXT_CONTENT}', safe_text)
else:
html_content = html_content.replace('{TEXT_CONTENT}', '')
html_content = html_content.replace('{ROWS_VALUE}', rows_value)
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
self.wfile.write(html_content.encode('utf-8'))
if __name__ == '__main__':
server_address = ('127.0.0.1', 5010)
httpd = HTTPServer(server_address, RequestHandler)
print(f'Server running at http://{server_address[0]}:{server_address[1]}')
httpd.serve_forever()