-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyserver.py
More file actions
34 lines (27 loc) · 1002 Bytes
/
myserver.py
File metadata and controls
34 lines (27 loc) · 1002 Bytes
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
# effectively, the world's simplest web server
from socket import *
def createServer():
serversocket = socket(AF_INET, SOCK_STREAM)
try:
serversocket.bind(('localhost', 9000))
serversocket.listen(5)
while True:
clientsocket, address = serversocket.accept()
rd = clientsocket.recv(5000).decode()
pieces = rd.split('\n')
if len(pieces) > 0:
print(pieces[0])
data = "HTTP/1.1 200 OK\r\n"
data += "Content-Type: text/html; charset=utf-8\r\n"
data += "\r\n"
data += "<html><body>Hello World</body></html>\r\n\r\n"
clientsocket.sendall(data.encode())
clientsocket.shutdown(SHUT_WR)
except KeyboardInterrupt:
print("\nShutting down\n")
except Exception as exc:
print("Error:\n")
print(exc)
serversocket.close()
print('Access http://localhost:9000')
createServer()