Created
March 7, 2024 17:47
-
-
Save yanweijia/457016422b3068ba34e52dc2fe90d8bc to your computer and use it in GitHub Desktop.
python http server 支持上传
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.server | |
import os | |
class FileUploadHandler(http.server.BaseHTTPRequestHandler): | |
def do_POST(self): | |
# 获取文件大小 | |
content_length = int(self.headers['Content-Length']) | |
# 读取客户端上传的文件数据 | |
file_data = self.rfile.read(content_length) | |
# 保存文件到服务器上 | |
with open('./file', 'wb') as f: | |
f.write(file_data) | |
self.send_response(200) | |
httpd = http.server.HTTPServer(('localhost', 8000), FileUploadHandler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment