Python开发【模块】:aiohttp(二)
AIOHTTP
1、文件上传
① 单个文件上传
服务端
async def post(self, request):
reader = await request.multipart()
# /!\ 不要忘了这步。(至于为什么请搜索 Python 生成器/异步)/!\
file = await reader.next()
filename = file.filename
# 如果是分块传输的,别用Content-Length做判断。
size = 0
with open(filename, 'wb') as f:
while True:
chunk = await file.read_chunk() # 默认是8192个字节。
if not chunk:
break
size += len(chunk)
f.write(chunk) return web.Response(text='{} sized of {} successfully stored'
''.format(filename, size))
客户端
import aiohttp
import asyncio url = 'http://127.0.0.1:8080/'
files = {'file': open('files/1M.wav', 'rb'),} async def fetch(session, url):
async with session.post(url,data=files) as response:
return await response.text() async def main():
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://127.0.0.1:8080')
print(html) loop = asyncio.get_event_loop()
loop.run_until_complete(main())
② 传输多个文件及其他参数
服务端
async def post(self, request):
reader = await request.multipart()
data = {}
async for read in reader:
filename = read.filename
if filename is not None:
size = 0
with open('./' + filename, 'wb') as f:
while True:
chunk = await read.read_chunk() # 默认是8192个字节。
if not chunk:
break
size += len(chunk)
f.write(chunk)
else:
value = await read.next()
key = read.name
data[key] = str(value, encoding='utf-8')
print(data)
return web.Response()
客户端
import aiohttp
import asyncio url = 'http://127.0.0.1:8080/'
files = {'file': open('files/1M.wav', 'rb'),
'file2': open('files/0.5M.wav', 'rb'),
'name':'000001',
} async def fetch(session, url):
async with session.post(url,data=files) as response:
return await response.text() async def main():
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://127.0.0.1:8080')
print(html) loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Python开发【模块】:aiohttp(二)的更多相关文章
- python标准模块(二)
本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...
- Python开发【十二章】:ORM sqlalchemy
一.对象映射关系(ORM) orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却 ...
- python开发模块基础:异常处理&hashlib&logging&configparser
一,异常处理 # 异常处理代码 try: f = open('file', 'w') except ValueError: print('请输入一个数字') except Exception as e ...
- python开发模块基础:os&sys
一,os模块 os模块是与操作系统交互的一个接口 #!/usr/bin/env python #_*_coding:utf-8_*_ ''' os.walk() 显示目录下所有文件和子目录以元祖的形式 ...
- python开发模块基础:序列化模块json,pickle,shelve
一,为什么要序列化 # 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化'''比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给?现在我们能想到的方法就是存在文 ...
- python开发模块基础:time&random
一,time模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块 常用方法1.(线程)推迟指定的时间运行.单位为秒. time.sleep(1) #括号内为整数 2.获取当前 ...
- python开发模块基础:collections模块¶miko模块
一,collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdic ...
- PYTHON开发--面向对象基础二
一.成员修饰符 共有成员 私有成员, __字段名 - 无法直接访问,只能间接访问 1. 私有成员 1.1 普通方法种的私有成员 class Foo: def __init__(self, n ...
- python开发模块基础:re正则
一,re模块的用法 #findall #直接返回一个列表 #正常的正则表达式 #但是只会把分组里的显示出来#search #返回一个对象 .group()#match #返回一个对象 .group() ...
- python开发模块基础:正则表达式
一,正则表达式 1.字符组:[0-9][a-z][A-Z] 在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表示字符分为很多类,比如数字.字母.标点等等.假如你现在要求一个位置&q ...
随机推荐
- DBNull与Null的区别
Null是.net中无效的对象引用. DBNull是一个类.DBNull.Value是它唯一的实例.它指数据库中数据为空(<NULL>)时,在.net中的值. null表示一个对象的指向无 ...
- Git 目录
linux通过用户名.密码提交的方式搭建私有git服务端 centos 6.5 6.6 6.7安装gitlab教程(社区版) Git 初始化项目.创建合并分支.回滚等常用方法总结 Git 错误集锦
- python3二元Logistics Regression 回归分析(LogisticRegression)
纲要 boss说增加项目平台分析方法: T检验(独立样本T检验).线性回归.二元Logistics回归.因子分析.可靠性分析 根本不懂,一脸懵逼状态,分析部确实有人才,反正我是一脸懵 首先解释什么是二 ...
- ViewPager PagerAdapter not updating the View
There are several ways to achieve this. The first option is easier, but bit more inefficient. Overri ...
- vector的多套遍历方案
1.迭代器 begin,end,*it++ 2.下标法 3.at函数(GetAt) 4.指针法 指针移到头部rewind.
- mybatis中批量插入以及更新
1:批量插入 批量插入就是在预编译的时候,将代码进行拼接,然后在数据库执行 <insert id="batchInsert" parameterType="java ...
- [Bayes] Metroplis Algorithm --> Gibbs Sampling
重要的是Gibbs的思想. 全概率分布,可以唯一地确定一个联合分布 ---- Hammersley-Clifford 多元高斯分布 当然,这个有点复杂,考虑个简单的,二元高斯,那么超参数就是: 二元高 ...
- [Laravel] 16 - DB: Eloquent
前言 一.大纲 写后端API,与数据库打交道无疑是很重要的角色. PHP数据库操作:从MySQL原生API到PDO PHP数据库操作:使用ORM Ref: [PHP] 07 - Json, XML a ...
- Unity 给Mono脚本添加Try Catch工具
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- 遍历DOM打平
html 模板 <div class="box"> <p>1</p> <p>2</p> <div> < ...