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


相关推荐
松涛和鸣6 分钟前
Linux Makefile : From Basic Syntax to Multi-File Project Compilation
linux·运维·服务器·前端·windows·哈希算法
再睡一夏就好1 小时前
深入Linux线程:从轻量级进程到双TCB架构
linux·运维·服务器·c++·学习·架构·线程
云和数据.ChenGuang1 小时前
OpenEuler系统下RabbitMQ安装与基础配置教程
服务器·分布式·rabbitmq·ruby·数据库运维工程师·运维教程
工控小楠1 小时前
EtherNET IP转Profinet协议网关在智能仓储系统中的应用
服务器·网络·tcp/ip
gaize12132 小时前
如何编写一个简单的服务器应用程序?
服务器·云计算
studytosky2 小时前
Linux系统编程:深度解析 Linux 进程,从底层架构到内存模型
linux·运维·服务器·开发语言·架构·vim
Stella25213 小时前
实习日志|知识总结
linux·服务器·软件测试·数据库
weixin_462446233 小时前
【原创实践】使用 shell 脚本批量创建 Linux 用户并生成随机密码
linux·服务器·前端
海奥华23 小时前
进程调度算法 笔记总结
linux·运维·服务器·笔记·学习
茉莉玫瑰花茶4 小时前
ProtoBuf - 3
服务器·c++·protobuf