`
sillycat
  • 浏览: 2486798 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Python Library 2019(1)requests and aiohttp

 
阅读更多
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/


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics