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()


相关推荐
吴声子夜歌28 分钟前
Nginx应用与运维——Nginx HTTP模块详解(数据处理功能模块)
运维·nginx·http
XiaoMaqqqq1 小时前
知名的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
2601_967287572 小时前
轮胎侧壁上的 91V 是什么意思,负荷指数与速度级别怎么看
运维·服务器·汽车
Ivanqhz3 小时前
层归一化、残差、前馈网络与激活函数简述
服务器·数据库·人工智能·深度学习·算法
ysu_03143 小时前
Postman接口调试实战指南-从HTTP请求到环境变量-Collection与自动化测试
网络·网络协议·测试工具·http·api·postman·可用性测试
小此方3 小时前
Linux网络(十九):TCP流量控制与滑动窗口详解,超时重传和快重传到底有什么区别
linux·运维·服务器·网络·网络协议·tcp/ip
沫璃染墨3 小时前
《从零入门Linux系统篇(五十四):线程篇·七——互斥锁底层原理:从原子交换到线程竞争与锁实现》
linux·运维·服务器·开发语言·c++·驱动开发·系统架构
吴声子夜歌4 小时前
Nginx应用与运维——Nginx HTTP模块详解(访问控制功能模块)
运维·nginx·http
网络小江4 小时前
组网第二课:X.25 与帧中继——第一次让带宽“拼车“
网络协议
Ruiery5 小时前
Linux 6.6内核 CPU 深度解析(一):BSP 拉起 — 从实模式到 kernel_init 的完整旅程
linux·运维·服务器