forked from furas/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-server.py
More file actions
48 lines (27 loc) · 1.02 KB
/
Copy pathsimple-server.py
File metadata and controls
48 lines (27 loc) · 1.02 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
#!/usr/bin/env python3
import socket
HOST, PORT = '', 8888
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# to solve problem with
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print(f'Serving HTTP on port {PORT} ...')
while True:
# ---
print('start ')
# --- accept new client ---
client_connection, client_address = listen_socket.accept()
# --- receive request ---
request_data = b''
while True:
request_data += client_connection.recv(1) # get by char to easier recognize end of header
if request_data.endswith(b'\r\n\r\n'): # recognize end of header - empty line
break
print(request_data.decode('utf-8'))
# --- send response ---
client_connection.sendall(b"""HTTP/1.1 200 OK\n\nHello, World 3@!\n""")
# --- close connection to client ---
client_connection.close()
# ----
print('end')