发布时间:2019-08-26 07:20:45编辑:auto阅读(1912)
使用python模拟http服务端,自定义返回信息头部:
#!/usr/bin/python
import os
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
Host = ''
Port = 8080
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
Time = os.popen('date +%Y-%m-%d_%H:%M:%S').read()
self.send_header('time','%s'%Time)
self.end_headers()
return
class CustomHTTPServer(HTTPServer):
def __init__(self,Host,Port):
server_address = (Host,Port)
HTTPServer.__init__(self,server_address,RequestHandler)
def run_server(p):
try:
server = CustomHTTPServer(Host,Port)
print "HTTPServer started on port:%s" % Port
server.serve_forever()
except Exception,err:
print "Error:%s" %err
except KeyboardInterrupt:
print "Server interrupted and is shutting down..."
server.socket.close()
if __name__ == "__main__":
run_server(Port)
上一篇: Python利用difflib比较字符串
下一篇: 用python获取MAC地址和IP地址
51276
50720
41323
38136
32597
29504
28355
23223
23191
21513
1588°
2308°
1919°
1861°
2186°
1902°
2592°
4334°
4190°
2985°