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


相关推荐
猿码优创20 分钟前
过滤境外ip和域名访问的解决方案
网络·网络协议·tcp/ip·安全·阿里云
北京耐用通信2 小时前
耐达讯自动化Profibus光纤链路模块:跨行业通信的“隐形桥梁”,让控制更丝滑!
人工智能·网络协议·自动化·信息与通信
归去来?2 小时前
记录一次从https接口提取25G大文件csv并落表的经历
大数据·数据仓库·hive·python·网络协议·5g·https
lkbhua莱克瓦242 小时前
深入理解HTTP协议:从理论到SpringBoot实践
网络·笔记·后端·网络协议·http·javaweb
newsxun3 小时前
从一条国道,到一个时代符号——申晨案例解析:如何打造公路文旅现象级IP「此生必驾318」
网络·网络协议·tcp/ip
JoyCong19983 小时前
高效远程协作指南:基于ToDesk的六大应用场景详解
运维·服务器·远程工作·远程操作
Bruce_Liuxiaowei4 小时前
基于抓包分析的组播视频流故障排查与解决
网络·网络协议·wireshark·信号处理
wniuniu_4 小时前
增加依据。。
服务器·网络·数据库
2501_915909064 小时前
设置了 SSL Pinning 与双向 TLS 验证要怎么抓包
网络·网络协议·ios·小程序·uni-app·iphone·ssl
txinyu的博客5 小时前
HTTPS
网络协议·http·https