项目作者: hidekuma

项目描述 :
🤖 Python Asynchronous cURL Requests
高级语言: Python
项目地址: git://github.com/hidekuma/asyncurl.git
创建时间: 2019-06-10T00:39:20Z
项目社区:https://github.com/hidekuma/asyncurl

开源协议:MIT License

下载


asyncurl Build Status

Asynchronous cURL Requests using python, which is inspired by this benchmark(KR, EN).

asyncurl-logo


Support python versions

python >= 3.6

Dependencies

AsyncURL project consists of the following packages:

Package Version Description
asyncio >=3.4.3 for Asynchronous
requests >=2.22.0 pycurl substitutes
uvloop >=0.12.2 for event loop policy

Installation

You can download asyncurl executable and binary distributions from PyPI.

Using pip

  1. pip install asyncurl

Usage

Import AsyncURL.

  1. from asyncurl.fetch import AsyncURLFetch
  2. ac_fetch = AsyncURLFetch()

Default worker’s count is 2. you can change it if you want.

  1. ac_fetch.worker = 3

and you can put urls to <AsyncURL.queue>.

  1. for x in range(2):
  2. ac_fetch.queue.put_nowait('http://localhost')
  3. ac_fetch.parallel()

Then call parallel(). The fucntion fetch urls using <requests>(HTTP library for Python).

and AsyncURL can change <requests>‘s method and else properties.

  1. from asyncurl.session import AsyncURLSession
  2. from asyncurl.fetch import AsyncURLFetch
  3. ac_fetch = AsyncURLFetch()
  4. for x in range(2):
  5. headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
  6. session = AsyncURLSession('GET', 'http://localhost', headers=headers)
  7. ac_fetch.queue.put_nowait(session)
  8. ac_fetch.parallel()

AsyncURLSession is inheritance of <requests.Session>.

parallel() will return <AsncURLFetch>, and it can show results to you.

Show results:

  1. ac_fetch.parallel().results

The order of result is nonsequential. and it will return list of <requests.Response>.

AsyncURLSession’s properties

equals to <requests.Request>

  • params=None
  • data=None
  • headers=None
  • cookies=None
  • files=None
  • auth=None
  • timeout=None
  • allow_redirects=True
  • proxies=None
  • hooks=None
  • stream=None
  • verify=None
  • cert=None
  • json=None

Examples

  1. for x in range(3):
  2. session = AsyncURLSession('GET', 'http://localhost')
  3. ac_fetch.queue.put_nowait(session)
  4. # case.1) with callback
  5. print('[with callback]')
  6. ac_fetch.parallel(callback=lambda x: print('with callback : {0}'.format(x)))
  7. # case.2) return results
  8. print('[return results]')
  9. print(ac_fetch.parallel().results)
  10. >>>
  11. [with callback]
  12. with callback : <Future finished result=<Response [403]>>
  13. with callback : <Future finished result=<Response [403]>>
  14. with callback : <Future finished result=<Response [403]>>
  15. [return results]
  16. [<Response [403]>, <Response [403]>, <Response [403]>]

License

The MIT License (MIT)

Copyright (c) 2019 Hidden function by hidekuma