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. Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand

    1 ItemDataBound:数据绑定的时候(正在进行时)发生. 2 ItemCommand :用来响应Item模板中的控件的事件. 如下代码 aspx代码: [html] view plain c ...

  2. mac中安装wxpython

    一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...

  3. Odoo 去掉 恼人的 "上午"和"下午"

  4. ESN,MEID 和pESN

    ESN (Electronic Serial Numbers):电子序列号.在CDMA 系统中,是鉴别一个物理硬件设备唯一的标识.也就是说每个手机都用这个唯一的ID来鉴别自己, 就跟人的身份证一样.一 ...

  5. recyclerView插入(add)和删除(remove)item后,item错乱,重复,覆盖在原recyclerView上

    项目用到,实现一个recyclerView列表的item翻转动效,翻转的同时会将指定item置顶. (比如交换AB位置,A在0位置,指定的item B 在 i 位置) 原始使用的是插入B到0位置,然后 ...

  6. AD域部署使用bginfo软件

    实验网络拓扑图: 实验目标: bginfo收集信息服务器通过bginfo软件收集每个域客户端信息录入到SQL server 2008数据库 bginfo软件官网下载地址: https://docs.m ...

  7. c# 正则匹配对称括号

    https://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis

  8. [Artoolkit] Can I Use LGPL code for commercial application

    这是一个比较普遍但又容易被忽略的问题. From: http://answers.google.com/answers/threadview/id/439136.html 假设背景: - want t ...

  9. 使用Python管理压缩包

    一. 使用tarfile库读取与创建tar包 1. 创建tar包 In [1]: import tarfile In [2]: with tarfile.open('demo.tar',mode='w ...

  10. Kafka ACL使用实战

    自0.9.0.0.版本引入Security之后,Kafka一直在完善security的功能.当前Kafka security主要包含3大功能:认证(authentication).信道加密(encry ...