http post协议发送本地压缩数据到服务器

1.客户端程序

python 复制代码
import requests
import os
# 指定服务器的URL
url = "http://192.168.1.9:8000/upload"

# 压缩包文件路径
folder_name = "upload"
file_name = "test.7z"
headers = {
    'Folder-Name': folder_name,
    'File-Name': file_name
}
# 发送POST请求,并将压缩包作为文件上传
with open(file_name, 'rb') as file:
    response = requests.post(url, data=file,headers=headers)
# 检查响应状态码
if response.status_code == 200:
    print("文件上传成功!")
else:
    print("文件上传失败!")
os.system('pause')


2.服务端程序

python 复制代码
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
# Define the request handler class
class MyRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        # Send response status code
        self.send_response(200)

        # Send headers
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        path = self.path
        if path == '/':
            response_content = b'Hello, world!'
        elif path == '/about':
            response_content = b'This is the about page.'
        else:
            response_content = b'Page not found.'

        # Send response content
        self.wfile.write(response_content)
    def do_POST(self):
        client_address = self.client_address[0]
        client_port = self.client_address[1]

        print("client from:{}:{}".format(client_address,client_port))
        path = self.path
        print(path)
        if path == '/upload':

            content_length = int(self.headers['Content-Length'])
            print(self.headers)
            filename = self.headers.get('File-Name')
            foldname = self.headers.get('Folder-Name')
            #print(filename)
            #print(foldname)
            # 如果目录不存在,创建目录
            os.makedirs(foldname, exist_ok=True)
            filename = os.path.join(foldname, filename)
            print(filename)
            # 读取请求体中的文件数据
            file_data = self.rfile.read(content_length)
            # # 保存文件
            with open(filename, 'wb') as file:
                file.write(file_data)
            # 发送响应
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'File received and saved.')
        else:
            self.send_response(404)
            self.end_headers()
# Specify the server address and port
host = '192.168.1.9'
port = 8000

# Create an HTTP server instance
server = HTTPServer((host, port), MyRequestHandler)
print("Server started on {}:{}".format(host,port))

# Start the server and keep it running until interrupted
server.serve_forever()


相关推荐
热爱生活的猴子2 小时前
阿里云服务器正确配置 Docker 国内镜像的方法
服务器·阿里云·docker
一只小鱼儿吖6 小时前
进程代理单窗口单IP技术:原理、应用与实现
网络·网络协议·tcp/ip
稳联技术6 小时前
Ethernet IP与Profinet共舞:网关驱动绿色工业的智慧脉动
网络·网络协议·tcp/ip
隆里卡那唔6 小时前
在dify中通过http请求neo4j时为什么需要将localhost变为host.docker.internal
http·docker·neo4j
计算机毕设定制辅导-无忧学长6 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
高兴达6 小时前
RPC框架--实现一个非常简单的RPC调用
网络协议·rpc·firefox
~山有木兮7 小时前
LiteHub中间件之限流实现
网络·http·中间件
游戏开发爱好者88 小时前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_915106328 小时前
频繁迭代下完成iOS App应用上架App Store:一次快速交付项目的完整回顾
websocket·网络协议·tcp/ip·http·网络安全·https·udp
cocologin9 小时前
RIP 技术深度解析
运维·网络·网络协议