Skip to content

Instantly share code, notes, and snippets.

@yanweijia
Created March 7, 2024 17:47
Show Gist options
  • Save yanweijia/457016422b3068ba34e52dc2fe90d8bc to your computer and use it in GitHub Desktop.
Save yanweijia/457016422b3068ba34e52dc2fe90d8bc to your computer and use it in GitHub Desktop.
python http server 支持上传
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