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(二)的更多相关文章

  1. python标准模块(二)

    本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...

  2. Python开发【十二章】:ORM sqlalchemy

    一.对象映射关系(ORM) orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却 ...

  3. python开发模块基础:异常处理&hashlib&logging&configparser

    一,异常处理 # 异常处理代码 try: f = open('file', 'w') except ValueError: print('请输入一个数字') except Exception as e ...

  4. python开发模块基础:os&sys

    一,os模块 os模块是与操作系统交互的一个接口 #!/usr/bin/env python #_*_coding:utf-8_*_ ''' os.walk() 显示目录下所有文件和子目录以元祖的形式 ...

  5. python开发模块基础:序列化模块json,pickle,shelve

    一,为什么要序列化 # 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化'''比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给?现在我们能想到的方法就是存在文 ...

  6. python开发模块基础:time&random

    一,time模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块 常用方法1.(线程)推迟指定的时间运行.单位为秒. time.sleep(1) #括号内为整数 2.获取当前 ...

  7. python开发模块基础:collections模块&paramiko模块

    一,collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdic ...

  8. PYTHON开发--面向对象基础二

    一.成员修饰符 共有成员 私有成员, __字段名 - 无法直接访问,只能间接访问 1.     私有成员 1.1  普通方法种的私有成员 class Foo: def __init__(self, n ...

  9. python开发模块基础:re正则

    一,re模块的用法 #findall #直接返回一个列表 #正常的正则表达式 #但是只会把分组里的显示出来#search #返回一个对象 .group()#match #返回一个对象 .group() ...

  10. python开发模块基础:正则表达式

    一,正则表达式 1.字符组:[0-9][a-z][A-Z] 在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表示字符分为很多类,比如数字.字母.标点等等.假如你现在要求一个位置&q ...

随机推荐

  1. Android——RecycleView

    RecycleView设置点击事件 http://blog.csdn.net/guxiao1201/article/details/40423361

  2. PyCharm 2018 最新激活方式总结(最新最全最有效!!!)

    PyCharm 2018 最新激活方式总结(最新最全最有效!!!) 欲善其事,必先利其器.这里我为大家提供了三种激活方式: 授权服务器激活:适合小白,一步到位,但服务器容易被封 激活码激活:适合小白, ...

  3. CSS初始化示例代码

    CSS初始化示例代码 /* css reset www.admin10000.com */ body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code, ...

  4. opencv_java import org.opencv.highgui.Highgui,类中无imread方法

    opencv_java import org.opencv.highgui.Highgui,提示错误 2018年01月19日 14:50:25 小码农的路程 阅读数:358   原因:1.OpenCV ...

  5. Sphinx 2.2.11-release reference manual

    1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...

  6. 【转】 Windows下配置Git

    [转自]http://blog.csdn.net/exlsunshine/article/details/18939329 1.从git官网下载windows版本的git:http://git-scm ...

  7. Excel公式中使用动态计算的地址

    例:统计A列第四行开始,到公式所在行的前一行的非空白行的个数: =COUNTA(A4:INDIRECT(ADDRESS(ROW()-,COLUMN())))

  8. [Math] Unconstrained & Constrained Optimization

    粘贴两个典型的例子,只是基础内容,帮助理解. (1) Solution: (2) Solution:

  9. [React] 08 - Tutorial: evolution of code-behind

    有了七篇基础学习,了解相关的知识体系,之后便是系统地再来一次. [React] 01 - Intro: javaScript library for building user interfaces ...

  10. tomcat 下安装 MantisBT

    环境 OS:win8.1 up1 64bit tomcat :9.0.0 64bit php: php-7.1.7-nts-Win32-VC14-x64.zip postgres: postgresq ...