How could I use requests in asyncio?

To use requests (or any other blocking libraries) with asyncio, you can use BaseEventLoop.run_in_executor to run a function in another thread and yield from it to get the result. For example: import asyncio import requests @asyncio.coroutine def main(): loop = asyncio.get_event_loop() future1 = loop.run_in_executor(None, requests.get, ‘http://www.google.com’) future2 = loop.run_in_executor(None, requests.get, ‘http://www.google.co.uk’) response1 = yield from … Read more

Asynchronous Requests with Python requests

Note The below answer is not applicable to requests v0.13.0+. The asynchronous functionality was moved to grequests after this question was written. However, you could just replace requests with grequests below and it should work. I’ve left this answer as is to reflect the original question which was about using requests < v0.13.0. To do … Read more

Requests — how to tell if you’re getting a 404

Look at the r.status_code attribute: if r.status_code == 404: # A 404 was issued. Demo: >>> import requests >>> r = requests.get(‘http://httpbin.org/status/404’) >>> r.status_code 404 If you want requests to raise an exception for error codes (4xx or 5xx), call r.raise_for_status(): >>> r = requests.get(‘http://httpbin.org/status/404’) >>> r.raise_for_status() Traceback (most recent call last): File “<stdin>”, line … Read more

How to upload file with python requests?

If upload_file is meant to be the file, use: files = {‘upload_file’: open(‘file.txt’,’rb’)} values = {‘DB’: ‘photcat’, ‘OUT’: ‘csv’, ‘SHORT’: ‘short’} r = requests.post(url, files=files, data=values) and requests will send a multi-part form POST body with the upload_file field set to the contents of the file.txt file. The filename will be included in the mime … Read more

SSL InsecurePlatform error when using Requests package

Use the somewhat hidden security feature: pip install requests[security] or pip install pyOpenSSL ndg-httpsclient pyasn1 Both commands install following extra packages: pyOpenSSL cryptography idna Please note that this is not required for python-2.7.9+. If pip install fails with errors, check whether you have required development packages for libffi, libssl and python installed in your system … Read more

Proxies with Python ‘Requests’ module

The proxies‘ dict syntax is {“protocol”: “scheme://ip:port”, …}. With it you can specify different (or the same) proxie(s) for requests using http, https, and ftp protocols: http_proxy = “http://10.10.1.10:3128” https_proxy = “https://10.10.1.11:1080” ftp_proxy = “ftp://10.10.1.10:3128” proxies = { “http” : http_proxy, “https” : https_proxy, “ftp” : ftp_proxy } r = requests.get(url, headers=headers, proxies=proxies) Deduced from … Read more

How to install packages offline?

On the system that has access to internet The pip download command lets you download packages without installing them: pip download -r requirements.txt (In previous versions of pip, this was spelled pip install –download -r requirements.txt.) On the system that has no access to internet Then you can use pip install –no-index –find-links /path/to/download/dir/ -r … Read more

Can I set max_retries for requests.request?

This will not only change the max_retries but also enable a backoff strategy which makes requests to all http:// addresses sleep for a period of time before retrying (to a total of 5 times): import requests from requests.adapters import HTTPAdapter, Retry s = requests.Session() retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[ 500, 502, 503, 504 ]) s.mount(‘http://’, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)