python web.py+requests 视频接收与发送

         web.py是python中一个相对容易上手的web服务器搭建工具。

1 安装方式

        web.py可以直接通过pip install 的方式安装即可,即:

pip install web.py

2 服务器

        2.1 完整程序

import web              #web.py

urls = (
        '/server' , 'server', 
        '/.*', 'notfound'     #localhost:port/其他任意界面,访问notfound类
        )

class MyApplication(web.application):
    def run(self, port=8080, *middleware):
        func = self.wsgifunc(*middleware)
        return web.httpserver.runsimple(func, ('0.0.0.0', port))

class server:
    def __init__(self):
        self.return_msg = {'errorCode': 0, 'msg': '系统正常!'}     

    def POST(self):                    #POST处理方式与GET一致
        content  = web.input()
        print('content keys: ', content.keys())
        print('收到消息:', content.imagename.decode(), content.imagetype.decode(), content.key1.decode())
        savedir = './'
        fout = open(savedir + '/' + 'test_recv.mp4', 'wb')
        fout.write(content.image)
        fout.close()
        return str(self.return_msg).replace('\'', '\"')
    
class notfound:
    def GET(self):
        print('--from notfound')
        return '404 not found'
    def POST(self):
        print('--from notfound')
        return '404 not found'
     

if __name__ == "__main__":
    app = MyApplication(urls ,globals())
    app.run(port=8091)

        2.2 url页面与响应类

        url页面是指网页访问地址,响应类是指定页面做出的响应。如上所示,url页面用一个小括号元组形式来定义。'/server', 'server' 表示url地址为127.0.0.1:port/server或者localhost:port/server页面对应函数处理类为class server。'/.*', 'notfound'表示除了server页面之外,且在指定端口port下的地址时均由class notfound类来表示。可以按照上述方法,定义多个页面。

        在响应函数类处理消息过程中,POST与GET处理方法基本一致。

urls = (
        '/server' , 'server', 
        '/.*', 'notfound'     #localhost:port/其他任意界面,访问notfound类
        )

        2.3 自定义端口

         web.py默认端口为8080端口,但是有时候8080已经被占用了,所以需要自定义端口。

        自定义端口的方式可以用两种方式来实现,第一种是在命令行运行脚本,采用如下方式:

python main.py 8090

        另一种方式是按照上述代码的方式,重载web.application类。

class MyApplication(web.application):
    def run(self, port=8080, *middleware):
        func = self.wsgifunc(*middleware)
        return web.httpserver.runsimple(func, ('0.0.0.0', port))
if __name__ == "__main__":
    app = MyApplication(urls ,globals())
    app.run(port=8090)

3 客户端

        3.1 完整程序

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 30 20:58:41 2022

@author: Administrator
"""

import requests

def client_post(url, data):
    rep = requests.post(url, files=files)
    return rep.text 

if __name__ == '__main__':
    files = {
            "imagename":"videdo server demo",
            "image":("1.mp4",open('E:/1.mp4','rb'),"application/octet-stream"),
            "imagetype":"jpg",
            "key1":"key1 content"
            }
    url = 'http://127.0.0.1:8091/server'
    res = client_post(url, files)
    print('127.0.0.1:8091/server(返回结果):', res)

4 测试结果

        python客户端运行结果如下:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding的叶子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值