-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_server.py
More file actions
52 lines (41 loc) · 1.37 KB
/
web_server.py
File metadata and controls
52 lines (41 loc) · 1.37 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
49
50
51
52
# coding=utf-8
import BaseHTTPServer
# 返回请求的信息
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
# Page模板
Page = '''\
<html>
<body>
<table>
<tr> <td>Header</td> <td>Value</td>
<tr> <td>date and time</td> <td>{date_time}</td>
<tr> <td>Client host</td> <td>{client_host}</td>
<tr> <td>Client port</td> <td>{client_port}</td>
<tr> <td>Command</td> <td>{command}</td>
<tr> <td>Path</td> <td>{path}</td>
'''
# 发送内容于客户端
def send_content(self, page):
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.send_header("Content-Length", str(len(page)))
self.end_headers()
self.wfile.write(page)
def do_GET(self):
page = self.create_page()
self.send_content(page)
# 页面的创建
def create_page(self):
values = {
'date_time':self.date_time_string(),
'client_host':self.client_address[0],
'client_port':self.client_address[1],
'command':self.command,
'path':self.path
}
page = self.Page.format(**values)
return page
if __name__ == "__main__":
serverAddress = ('', 8000)
server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler)
server.serve_forever()