Python: grequest and request give different response

PMvn

My original task: Using Trello API, get data through HTTP GET requests. Run requests and process responses asynchronously, if possible. The API provider uses "https://" URL I access to with some key and token.

Tools I used:

  • Python 2.7.10 | Anaconda 2.3.0 (64-bit) | (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)] on win32
  • requests library (just imported without installation)
  • grequests library (installed via pip from this git repo)

Original task result: Only requests library worked, I've got Trello API's response, great. grequests library was failing with status_code = 302.

I tried to understand why it happens and wrote two reproducible scripts.

Script A : requests library used:

import requests

urls = [
    "https://www.google.com",
    "https://www.facebook.com/",
    "http://www.facebook.com/",
    "http://www.google.com",
    "http://fakedomain/",
    "http://python-tablib.org"
]

# Run requests:
for url in urls:
    print requests.get(url).status_code

Console output A (having some exception because of http://fakedomain/) :

200
200
200
200
Traceback (most recent call last):
  File "req.py", line 14, in <module>
    print requests.get(url).status_code
  File "D:\python\lib\site-packages\requests\api.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python\lib\site-packages\requests\api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "D:\python\lib\site-packages\requests\adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(11001, 'getaddrinfo failed'))

Script B : grequests library used with map to send asynchronous requests:

import grequests

# This function will execute set of instructions when responses come:
def proc_response(response, **kwargs):
    # do something ..
    print response

# Request exception handler:
def my_except_handler(request, excetion):
    print "Request failed : " + request.url

urls = [
    "https://www.google.com",
    "https://www.facebook.com/",
    "http://www.facebook.com/",
    "http://www.google.com",
    "http://fakedomain/",
    "http://python-tablib.org"
]
# Here is the list of tasks we build and run in parallel later:
actions_list = []

# Tasks list building:
for url in urls:
    action_item = grequests.get(url, hooks = {'response' : proc_response})
    actions_list.append(action_item)

# Run grequests:
print grequests.map(actions_list, exception_handler=my_except_handler)

Console output B :

<Response [302]>
<Response [302]>
<Response [200]>
<Response [301]>
<Response [302]>
<Response [200]>
Request failed : https://www.google.com
Request failed : https://www.facebook.com/
Request failed : http://www.facebook.com/
Request failed : http://fakedomain/
[None, None, None, <Response [200]>, None, <Response [200]>]

All I can conclude based on this information and my relatively small experience is the following - because of some reason grequests is rejected by remote websites requests works normally with. As long as 302 means redirection of some kind, it seems that grequests can not get data from source it is redirected to when requests can. allow_redirects=True in get method in Script B didn't solve the issue.

I wonder why libraries give different response. It is possible that I miss something, and these two scripts have to return different results by design, not because of differences between two libraries.

Thanks for your help in advance.

Jan Vlcinsky

grequests works well for me

Here is my script b.py, which I run via $ py.test -sv b.py:

import pytest
import grequests


@pytest.fixture
def urls():
    return [
        "https://www.google.com",
        "https://www.facebook.com/",
        "http://www.facebook.com/",
        "http://www.google.com",
        "http://fakedomain/",
        "http://python-tablib.org"
    ]


# This function will execute set of instructions when responses come:
def proc_response(response, **kwargs):
    # do something ..
    print "========Processing response=============", response.request.url
    print response
    if response.status_code != 200:
        print response.request.url
        print response.content


# Request exception handler:
def my_except_handler(request, exception):
    print "Request failed : " + request.url
    print request.response


def test_it(urls):
    # Here is the list of tasks we build and run in parallel later:
    actions_list = []

    # Tasks list building:
    for url in urls:
        action_item = grequests.get(url, hooks={'response': proc_response})
        actions_list.append(action_item)

    # Run grequests:
    print grequests.map(actions_list, exception_handler=my_except_handler)

It is based on your code, it is only rewritten to easy my experimentation.

Results: Final results are 200 or None

Last printout of my test shows:

[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, None, <Response [200]>]

This is what is expected.

Note, that you could have some temporary problems with fetching the data, there are too many players participating.

Conclusion: different response processing confused you

The difference is, that with requests you are asking for final result while with grequests you deploy process_response hook, which is called for each response including redirect ones.

The requests processing goes through redirect too, but this temporary response is not reported.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Each request of ajax give the data in response

From Dev

HTTP Request Response in Different Flow

From Dev

HTTP Request Response in Different Flow

From Dev

HTTP request / response with response from a different server

From Dev

i have different responses to each answer i give. my code prints more than one response. Python

From Dev

Is it bad to create different classes for REST request and response?

From Dev

Is it bad to create different classes for REST request and response?

From Dev

HTTP Response content type different on HEAD request

From Dev

Restful - same request but difference response on different scenarios

From Dev

Debugging a request/response in Python flask

From Dev

Python Scapy --arp request and response

From Dev

Python Scapy --arp request and response

From Dev

python post request not generating response

From Dev

Go and Python HMAC libraries give different results

From Dev

hashing different tuples in python give identical result

From Dev

Python interpreter and pip give different versions of module

From Dev

Same equations give different answers : PYTHON

From Dev

Java and Python codes give different output?

From Dev

Web-Api: When getting a request - give a response but then continue do a job related to the request

From Dev

Different Python minimization functions give different values, Why?

From Dev

How give different colors for different group of barplots in python?

From Dev

Response' object is not subscriptable Python http post request

From Dev

Webscraping with Request in Python- Script Response

From Dev

No response from site of get request in python 3

From Dev

Is it bad practice for a REST endpoint to return different response fields based on the request?

From Dev

More [GET] request-response examples with different URI parameters

From Dev

Scrapy response is a different language from request and resposne url

From Dev

Property Transfer from a testcase response to a request in different testcase

From Dev

Response of ajax request is different on my local environment and my live server

Related Related

  1. 1

    Each request of ajax give the data in response

  2. 2

    HTTP Request Response in Different Flow

  3. 3

    HTTP Request Response in Different Flow

  4. 4

    HTTP request / response with response from a different server

  5. 5

    i have different responses to each answer i give. my code prints more than one response. Python

  6. 6

    Is it bad to create different classes for REST request and response?

  7. 7

    Is it bad to create different classes for REST request and response?

  8. 8

    HTTP Response content type different on HEAD request

  9. 9

    Restful - same request but difference response on different scenarios

  10. 10

    Debugging a request/response in Python flask

  11. 11

    Python Scapy --arp request and response

  12. 12

    Python Scapy --arp request and response

  13. 13

    python post request not generating response

  14. 14

    Go and Python HMAC libraries give different results

  15. 15

    hashing different tuples in python give identical result

  16. 16

    Python interpreter and pip give different versions of module

  17. 17

    Same equations give different answers : PYTHON

  18. 18

    Java and Python codes give different output?

  19. 19

    Web-Api: When getting a request - give a response but then continue do a job related to the request

  20. 20

    Different Python minimization functions give different values, Why?

  21. 21

    How give different colors for different group of barplots in python?

  22. 22

    Response' object is not subscriptable Python http post request

  23. 23

    Webscraping with Request in Python- Script Response

  24. 24

    No response from site of get request in python 3

  25. 25

    Is it bad practice for a REST endpoint to return different response fields based on the request?

  26. 26

    More [GET] request-response examples with different URI parameters

  27. 27

    Scrapy response is a different language from request and resposne url

  28. 28

    Property Transfer from a testcase response to a request in different testcase

  29. 29

    Response of ajax request is different on my local environment and my live server

HotTag

Archive