- 浏览: 2542396 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Python Library 2019(1)requests and aiohttp
Try AIOHttp
> mkdir aiohttp
> cd aiohttp/
Check Version
> python -V
Python 3.7.5
> pip -V
pip 19.3.1 from /Users/hluo/.pyenv/versions/3.7.5/lib/python3.7/site-packages/pip (python 3.7)
> pip install aiohttp
> pip install cchardet
> pip install aiodns
Write a simple HTTP Server
> cat web.py
import asyncio
from aiohttp import web
routes = web.RouteTableDef()
@routes.get('/')
async def index(request):
await asyncio.sleep(2)
return web.json_response({
'name': 'index'
})
@routes.get('/about')
async def about(request):
await asyncio.sleep(0.5)
return web.Response(text="<h1>about us</h1>")
def init():
app = web.Application()
app.add_routes(routes)
web.run_app(app)
init()
Start the server, but it complains in 3.7.x python version
> python web.py
Downgrade the python version to 3.6.x
> pyenv local 3.6.8
> python -V
Python 3.6.8
Start the server again
> python web.py
Then we can visit these URLs
http://localhost:8080/about
http://localhost:8080/
Check library requests
> pip install requests
Get Sample
>>> import requests
>>> playload={'page':'1','per_page':'10'}
>>> r = requests.get("http://httpbin.org/get", params=playload)
>>> print(r.url)
http://httpbin.org/get?page=1&per_page=10
Post the JSON information
>>> import json
>>> import requests
>>> playload={'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", data=json.dumps(playload))
>>> print(r.text)
{
"args": {},
"data": "{\"page\": 1, \"per_page\": 10}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "27",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"json": {
"page": 1,
"per_page": 10
},
"origin": "207.114.244.5, 207.114.244.5",
"url": "https://httpbin.org/post"
}
Alternative way to do the JSON
>>> import requests
>>> playload = { 'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", json=playload)
>>> print(r.text)
Add to the Headers
>>> import requests
>>> url = 'http://httpbin.org/post'
>>> payload = { 'page': 1, 'per_page': 10 }
>>> headers = { 'User-Agent': 'Python Script' }
>>> r = requests.post(url, json=payload, headers=headers)
>>> print(r.request.headers)
{'User-Agent': 'Python Script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '27', 'Content-Type': 'application/json'}
>>> print(r.headers)
{'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Mon, 16 Dec 2019 20:04:28 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '264', 'Connection': 'keep-alive'}
Working on Response
>>> import requests
>>> r = requests.get("http://httpbin.org/get")
>>> print(r.status_code)
200
Get JSON response
>>> import requests
>>> r = requests.get("https://github.com/timeline.json")
>>> print(r.json())
{'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}
Session to Keep the Cookie
>>> import requests
>>> s = requests.Session()
>>> s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
<Response [200]>
>>> r = s.get("http://httpbin.org/cookies")
>>> print(r.text)
{
"cookies": {
"sessioncookie": "123456789"
}
}
Session help send Default Parameters
>>> import requests
>>> s = requests.Session()
>>> s.auth = ('user','password')
>>> s.headers.update({'api-key':'adsfsadfsdf'})
>>> r = s.get('http://httpbin.org/headers', headers={'User-Agent':'python script'})
>>> print(r.request.headers)
{'User-Agent': 'python script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'api-key': 'adsfsadfsdf', 'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}
References:
https://www.jianshu.com/p/acb6a1b95fcb
https://aiohttp.readthedocs.io/en/stable/
Try AIOHttp
> mkdir aiohttp
> cd aiohttp/
Check Version
> python -V
Python 3.7.5
> pip -V
pip 19.3.1 from /Users/hluo/.pyenv/versions/3.7.5/lib/python3.7/site-packages/pip (python 3.7)
> pip install aiohttp
> pip install cchardet
> pip install aiodns
Write a simple HTTP Server
> cat web.py
import asyncio
from aiohttp import web
routes = web.RouteTableDef()
@routes.get('/')
async def index(request):
await asyncio.sleep(2)
return web.json_response({
'name': 'index'
})
@routes.get('/about')
async def about(request):
await asyncio.sleep(0.5)
return web.Response(text="<h1>about us</h1>")
def init():
app = web.Application()
app.add_routes(routes)
web.run_app(app)
init()
Start the server, but it complains in 3.7.x python version
> python web.py
Downgrade the python version to 3.6.x
> pyenv local 3.6.8
> python -V
Python 3.6.8
Start the server again
> python web.py
Then we can visit these URLs
http://localhost:8080/about
http://localhost:8080/
Check library requests
> pip install requests
Get Sample
>>> import requests
>>> playload={'page':'1','per_page':'10'}
>>> r = requests.get("http://httpbin.org/get", params=playload)
>>> print(r.url)
http://httpbin.org/get?page=1&per_page=10
Post the JSON information
>>> import json
>>> import requests
>>> playload={'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", data=json.dumps(playload))
>>> print(r.text)
{
"args": {},
"data": "{\"page\": 1, \"per_page\": 10}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "27",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"json": {
"page": 1,
"per_page": 10
},
"origin": "207.114.244.5, 207.114.244.5",
"url": "https://httpbin.org/post"
}
Alternative way to do the JSON
>>> import requests
>>> playload = { 'page':1, 'per_page':10}
>>> r = requests.post("http://httpbin.org/post", json=playload)
>>> print(r.text)
Add to the Headers
>>> import requests
>>> url = 'http://httpbin.org/post'
>>> payload = { 'page': 1, 'per_page': 10 }
>>> headers = { 'User-Agent': 'Python Script' }
>>> r = requests.post(url, json=payload, headers=headers)
>>> print(r.request.headers)
{'User-Agent': 'Python Script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '27', 'Content-Type': 'application/json'}
>>> print(r.headers)
{'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Mon, 16 Dec 2019 20:04:28 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '264', 'Connection': 'keep-alive'}
Working on Response
>>> import requests
>>> r = requests.get("http://httpbin.org/get")
>>> print(r.status_code)
200
Get JSON response
>>> import requests
>>> r = requests.get("https://github.com/timeline.json")
>>> print(r.json())
{'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}
Session to Keep the Cookie
>>> import requests
>>> s = requests.Session()
>>> s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
<Response [200]>
>>> r = s.get("http://httpbin.org/cookies")
>>> print(r.text)
{
"cookies": {
"sessioncookie": "123456789"
}
}
Session help send Default Parameters
>>> import requests
>>> s = requests.Session()
>>> s.auth = ('user','password')
>>> s.headers.update({'api-key':'adsfsadfsdf'})
>>> r = s.get('http://httpbin.org/headers', headers={'User-Agent':'python script'})
>>> print(r.request.headers)
{'User-Agent': 'python script', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'api-key': 'adsfsadfsdf', 'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='}
References:
https://www.jianshu.com/p/acb6a1b95fcb
https://aiohttp.readthedocs.io/en/stable/
发表评论
-
Stop Update Here
2020-04-28 09:00 310I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 468NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 362Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 364Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 330Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 424Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 366Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 445VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 377Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 468NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 414Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 331Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 243GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 445GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 321GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 307Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 286Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 303Serverless with NodeJS and Tenc ...
相关推荐
`requests` 是一个同步库,适合执行单线程请求,而 `aiohttp` 是一个异步库,基于Python的异步I/O模型(通常称为asyncio),在处理大量并发请求时性能更优。 首先,让我们深入理解这两个库的工作原理: 1. **...
1. `python-3.7.4-amd64.exe`: 这是Python 3.7.4的安装程序,适用于AMD64架构的计算机。Python是requests库的基础,必须先安装Python环境才能使用requests。 2. `certifi-2019.9.11-py2.py3-none-any.whl`: certifi...
python3的 requests离线安装包,仅window环境能用,离线安装方法如下: 解压到任意目录后,cd到requests目录 然后 pip install --no-index --find-links=pack -r requirements.txt 或者 python -m pip install --no-...
1. 首先,确保已经安装了`aiohttp`库。如果尚未安装,可以使用`pip install aiohttp`命令进行安装。 2. 导入`aiohttp`库中的`ClientSession`和`asyncio`库的`asyncio.run`(对于Python 3.7以下版本,应使用`asyncio....
Python的`requests`模块是用于发送HTTP请求的强大库,它简化了与Web服务的交互,使得开发者能够方便地获取网页内容、提交表单、下载文件等。本压缩包包含`requests`模块本身及其依赖包,如`certifi`、`chardet`等,...
the Python Requests library is one of the world's best clients, with the highest number of downloads. It allows hassle-free interactions with web applications using simple procedures. You will be ...
在Python的网络编程中,`requests`库是一个广泛使用的HTTP客户端库,因其简洁易用而深受开发者喜爱。本文将深入探讨如何构建高效的`requests`长连接池,这对于处理大量HTTP请求,尤其是涉及到频繁的API调用或者CDN...
《Python库requests_pkcs12详解》 在Python的开发世界中,有一个强大的库叫做`requests`,它使得网络请求变得简单易用。然而,在处理需要使用PKCS#12证书的安全HTTPS通信时,`requests`原生并不支持。为了解决这个...
python实现接口自动化测试: 1、测试框架:python+unittest+requests+ddt数据驱动 2、测试用例维护在excle 3、支持post方法、get方法等 4、支持测试报告结果发送至qq邮箱
1. Python中的requests库:Python开发人员通常使用requests库来发送HTTP请求。requests库提供了一种简单易用的方法来处理HTTP/1.1协议,使用起来直观且方便。然而,requests的默认行为是串行发送请求,这意味着一次...
1. **Requests库**:这是Python中最常用的HTTP客户端库,用于发送HTTP请求。它可以方便地实现GET、POST、PUT、DELETE等各种HTTP方法,处理cookies、文件上传、HTTP认证等功能。在接口测试中,Requests库是与服务器...
1. 使用`pip`(Python包管理器):在命令行中输入以下命令: ``` pip install requests ``` 这样就能自动下载并安装`requests`库及其依赖。 2. 如果你有`requests`的源代码,如压缩包中的`requests-master`,你...
python requests官方中文文档,进阶用法。本文档覆盖了requests库的一些高级特性
本系统是一个主要使用python3, celery和requests来爬取职位数据的爬虫,实现了定时任务,出错重试,日志记录,自动更改Cookies等的功能,并使用ECharts Bootstrap 来构建前端页面,来展示爬取到的数据。
基于Python+Flask、requests的抖音去水印工具-毕业设计源码+使用文档(高分优秀项目) 该项目是个人高分毕业设计项目源码,已获导师指导认可通过,答辩评审分达到97分,在window10/11测试环境严格调试,下载即用,...
「Python系列」Python requests模块
本项目利用Python3作为主要编程语言,结合unittest测试框架,requests库处理HTTP请求,以及Excel作为数据驱动,构建了一个接口自动化测试框架。下面将详细解析这个框架的核心组成部分及实现原理。 **unittest测试...
python+requests+pytest+allure+yaml 一、数据类型处理 “${read_extract_data(tag_id)}” 替换成 110 二、DDT数据驱动封装 数据驱动可以利用 excel,csv,yaml 用excel,csv的缺点 1.数据类型难处理 2.调用函数不是...