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


相关推荐
weixin_440730502 分钟前
03功能测试实战
服务器·功能测试·p2p
头发还没掉光光11 分钟前
Linux网络之TCP协议
linux·运维·开发语言·网络·网络协议·tcp/ip
尼古拉斯·纯情暖男·天真·阿玮12 分钟前
实验七 防火墙与入侵防护实验
linux·服务器·网络
lytao12312 分钟前
Docker磁盘空间自动化清理实战
运维·服务器·docker·自动化
AirDroid_cn17 分钟前
Realme怎样远程控制oppo?手机自带的功能可以实现吗?
运维·服务器·网络
咕噜咕噜啦啦17 分钟前
Windows安装CVAT
linux·服务器·opencv
负二代0.021 分钟前
Linux的进程管理
linux·运维·服务器
IT 行者23 分钟前
Spring Security 7 之 OIDC /connect/userinfo 端点解析:ID Token 与用户信息获取
linux·服务器·spring
慧一居士24 分钟前
同一个服务器上不同的域名跳往不同的前端项目页面,不显示端口号 ngnix根据不同域名跳转
运维·服务器·前端
筑梦之路25 分钟前
linux XFS文件系统误删救星——筑梦之路
linux·运维·服务器