-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] Grequests

, 05 2017 . 23:04 +
Requests , grequests . , , HTTP- requests, , .

, , threading gevent.

grequests, requests. gevent + requests. , .

Grequests requests.

POST- url-:

import grequests 
with open("C:\\path\\urls.txt") as werewolves: 
    array = [row.strip() for row in werewolves]

params = {'a':'b', 'c':'d'}
rs = [grequests.post(u, data=params) for u in array]
for r in grequests.imap(rs, size=16) 
    print(r[0].status_code, r[0].url)

, , , , params a:b, c:d.

rs POST-, r grequests.map([rs], size= , http-, 16 ).

r, grequests.imap() requests.

status code, url address, rs , :

TypeError: 'Response' object does not support indexing

traceback , :

def exception_handlerr(request, exception):
    print("Request failed", request.url) 

import grequests 
with open("C:\\path\\urls.txt") as werewolves: 
    array = [row.strip() for row in werewolves]

params = {'a':'b', 'c':'d'}
rs = [grequests.post(u, data=params) for u in array]
for r in grequests.map([rs], size=16, exception_handler=exception_handlerr) 
    print(r[0].status_code, r[0].url)


r , .
, . ܻ . , . Aiohttp.

grequests. grequests error- requests, :

def send(self, **kwargs):
    """
    Prepares request based on parameter passed to constructor and optional      ``kwargs```.
    Then sends request and saves response to :attr:`response`

    :returns: ``Response``
    """
    merged_kwargs = {}
    merged_kwargs.update(self.kwargs)
    merged_kwargs.update(kwargs)
    try:
        self.response = self.session.request(self.method,
                                            self.url, **merged_kwargs)
    except Exception as e:
        self.exception = e
        self.traceback = traceback.format_exc()
    return self

exception_handler:

def exception_handlerr(request, exception):
    print("Request failed", request.url) 
    # print(str(exception)) 


:

def exception_handlerr(request, exception):
    print("Request failed", request.url) 

import grequests 
with open("C:\\path\\urls.txt") as werewolves: 
    array = [row.strip() for row in werewolves]

params = {'a':'b', 'c':'d'}
rs = [grequests.post(u, data=params) for u in array]
for r in grequests.map([rs], size=16, exception_handler=exception_handlerr) 
    print(r.status_code, r.url)


.

GET- - POST-:

def exception_handlerr(request, exception):
    print("Request failed", request.url) 

import grequests 
with open("C:\\path\\urls.txt") as werewolves: 
    array = [row.strip() for row in werewolves]

params = {'a':'b', 'c':'d'}
rs = [grequests.get(u) for u in array]
for r in grequests.map([rs], size=16, exception_handler=exception_handlerr) 
    print(r.status_code, r.url)


grequests.
requests.

!
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/334970/

:  

: [1] []
 

:
: 

: ( )

:

  URL