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 ...
随机推荐
- 【WPF】自定义形状的按钮Button
需求:做一个如下图所示的多边形按钮. <!-- 特殊形状的按钮 --> <Grid> <Polygon Points="0,0 140,0 190,42 140 ...
- 学习MongoDB(三) Add an Arbiter to Replica Set 集群中加入仲裁节点
Add an Arbiter to Replica Set 在集群中加入仲裁节点,当集群中主节点挂掉后负责选出新的主节点,仲裁节点也是一个mongo实力,但是它不存储数据. 1.仲裁节点消耗很小的资源 ...
- Java知多少(70)面向字节流的应用
文件输入输出流 文件输入输出流 FileInputStream 和 FileOutputStream 负责完成对本地磁盘文件的顺序输入输出操作. [例 10-5]通过程序创建一个文件,从键盘输入字符, ...
- MyBatis Generator使用com.mysql.cj.jdbc.Driver遇到的问题
MyBatis Generator使用com.mysql.cj.jdbc.Driver Mybatis Generator 1.3.5 新建了一个decision库,并创建了一张user表 impor ...
- 6、二、App Components(应用程序组件):1、Intents and Intent Filters(意图和意图过滤器)
1.Intents and Intent Filters(意图和意图过滤器) 1.0.Intents and Intent Filters(意图和意图过滤器) An Intent is a messa ...
- Linux下常用的文件传输方式介绍与比较
参考链接:http://mingxinglai.com/cn/2014/03/copy-file-in-linux/ 本文介绍了linux之间传输文件的几种方式,并通过具体实验测试了几种文件传输方式之 ...
- 关于git CRLF LF结尾的问题
在使用git的过程中,如果我们的项目是跨平台开发的 那么CRLF的处理也许会成为一个很头疼的事情,有可能会出以下的莫名其妙的问题: 我们的某个开发人员在linux上提交的一个文件 当从windows上 ...
- SQL创建索引
http://www.w3school.com.cn/sql/sql_create.asp 注释:更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新.因此,理想的做法 ...
- 在WPS中删除整行的快捷键是什么?
选中需要删除的行,(方法:点击最左侧的行号):按快捷键Ctrl+-(按着Ctrl不放,再按小键盘的减号“-”),“-”是删除,“+”是插入,选中行,是对行操作,选中列就是对列操作,选中单元格,就是单元 ...
- [原]Jenkins(三)---Jenkins初始配置和插件配置
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. *版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/5331 ...