forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
40 lines (32 loc) · 999 Bytes
/
server.py
File metadata and controls
40 lines (32 loc) · 999 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
35
36
37
38
39
40
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: XuMing <[email protected]>
@description:
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
class RequestHandler(BaseHTTPRequestHandler):
Page = '''<html>
<head><title>Title is here.</title></head>
<body>
<p>Hello, web!</p>
</body>
</html>
'''
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html".encode())
self.send_header("Content-Length", str(len(self.Page)))
self.end_headers()
self.wfile.write(self.Page.encode())
if __name__ == '__main__':
serverAddress = ('localhost', 9000)
appServer = HTTPServer(serverAddress, RequestHandler)
print(time.asctime(), "server starts : %s:%s" % (serverAddress))
try:
appServer.serve_forever()
except KeyboardInterrupt:
pass
appServer.server_close()
print(time.asctime(), "server stops : %s:%s" % (serverAddress))