File tree 2 files changed +26
-77
lines changed
turtle/pattern-rectangle-circle
2 files changed +26
-77
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+
1
3
import socket
2
4
3
5
HOST , PORT = '' , 8888
4
6
5
7
listen_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
8
+
9
+ # to solve problem with
6
10
listen_socket .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
11
+
7
12
listen_socket .bind ((HOST , PORT ))
8
13
listen_socket .listen (1 )
9
14
10
15
print (f'Serving HTTP on port { PORT } ...' )
11
16
12
17
while True :
13
- print ('1 - start')
14
- client_connection, client_address = listen_socket.accept()
18
+
19
+ # ---
20
+
21
+ print ('start ' )
22
+
23
+ # --- accept new client ---
15
24
25
+ client_connection , client_address = listen_socket .accept ()
26
+
27
+ # --- receive request ---
28
+
16
29
request_data = b''
17
30
18
31
while True :
19
- chunk = client_connection.recv(1)
20
- request_data += chunk
21
- print(chunk)
22
- if request_data.endswith(b'\r\n\r\n'):
32
+ request_data += client_connection .recv (1 ) # get by char to easier recognize end of header
33
+ if request_data .endswith (b'\r \n \r \n ' ): # recognize end of header - empty line
23
34
break
24
35
25
36
print (request_data .decode ('utf-8' ))
26
37
38
+ # --- send response ---
39
+
27
40
client_connection .sendall (b"""HTTP/1.1 200 OK\n \n Hello, World 3@!\n """ )
41
+
42
+ # --- close connection to client ---
43
+
28
44
client_connection .close ()
29
- print ('2 - end')
45
+
46
+ # ----
47
+
48
+ print ('end' )
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments