码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • http post协议发送本地压缩数据到服务器


    1.客户端程序

    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')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述
    在这里插入图片描述

    2.服务端程序

    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    嵌入式人工智能(OpenCV-基于树莓派的人脸识别与入侵检测)
    R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、设置可视化图像的主题为theme_bw
    postgresql|数据库|序列Sequence的创建和管理
    基于springboot+vue的青年公寓服务平台
    Redis应用案例之优惠券秒杀
    【C++动态规划 多重背包】1774. 最接近目标价格的甜点成本|1701
    idea中提示:error has occurred, please check your installation and try again
    HIT 模式识别 手写汉字分类 Python实现
    Hive 分桶 Bucket
    Linux控制---进程程序替换
  • 原文地址:https://blog.csdn.net/Wwc_code/article/details/133966973
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号