Python 官方文档:入门教程 => 点击学习
使用python模拟Http服务端,自定义返回信息头部: #!/usr/bin/Python import os from Basehttpserver import BaseHTTPRequestHandler,HTTPServer
使用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)
--结束END--
本文标题: python 模拟HTTP服务端
本文链接: https://www.lsjlt.com/news/186343.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0