代码拉取完成,页面将自动刷新
import socketserver
import threading
import time
import sys
class RequestHandler(socketserver.StreamRequestHandler):
def handle(self):
filename = self.rfile.readline().strip().decode('UTF-8')
print("local file server is getting '{}' for {}.".format(filename, self.client_address[0]))
with open("{}/{}".format(self.server.root_dir, filename), "rb") as f:
self.wfile.write(f.read())
self.wfile.close()
class TcpFileServer:
def __init__(self, root_dir='.'):
HOST, PORT = '', 0
self.server = socketserver.TCPServer((HOST, PORT), RequestHandler)
self.server.root_dir = root_dir
self.ip, self.port = self.server.server_address
def __enter__(self):
self.run()
return self
def run(self):
self.server_thread = threading.Thread(target=self.server.serve_forever)
self.server_thread.daemon = True
self.server_thread.start()
print("local file server is runing on {}:{}. root='{}'".format(self.ip, self.port, self.server.root_dir))
def __exit__(self, exc_type, exc_val, exc_tb):
self.server.shutdown()
self.server.server_close()
if __name__ == "__main__":
root_dir = '.' if len(sys.argv) <= 1 else sys.argv[1]
with TcpFileServer(root_dir):
while True:
time.sleep(10)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。