-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
47 lines (30 loc) · 1.47 KB
/
example.py
File metadata and controls
47 lines (30 loc) · 1.47 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
import socket
import PythonHtmlCon
LISTEN_PORT = 8910 # The port
HTML_DATA = {"header": "Welcome to the example!", "color": "red", "txt": "Having fun!",
"type": "span", "var": False, "var2": False, "n1": False} # You can play around these
def rewrite(data: str, vars: dict = {}):
edited_file = PythonHtmlCon.DataConnection()
edited_file = edited_file.edit_variables_data(data, vars)
return edited_file
def analise_request(request: str):
if "GET" in request or "POST" in request and "HTTP" in request:
with open(".\\somehtml.html", "r") as f:
return "HTTP/1.1 200 OK\nContent-Type: text/html\n\n{}".format(rewrite(f.read(), HTML_DATA)) # Builds the response
return "HTTP/1.1 404 NOT FOUND\nConnection: close\n\nClosed!"
# To connect to the server please enter to the url of your browser: localhost:port ~ Default is 8910
def main():
# A server for helping out with the library
listening_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ("localhost", LISTEN_PORT)
listening_sock.bind(server_address)
listening_sock.listen(5)
while True:
client_soc, client_address = listening_sock.accept()
print("Client Connected!")
client_msg = client_soc.recv(2048).decode()
client_soc.sendall(analise_request(client_msg).encode())
client_soc.close()
listening_sock.close()
if __name__ == '__main__':
main()