Пример #1
0
def follow_next_redirect(client_resp, timeout=TIMEOUT):
    rreq = client_resp.raw_request
    resp = client_resp.response
    status_code = resp.status_code

    if not is_supported_redirect(status_code):
        raise ValueError('not followable')

    # get new method
    if status_code in (301, 302, 303):
        new_method = 'HEAD' if rreq.method == 'HEAD' else 'GET'
    elif status_code in (307, 308):
        raise ValueError()

    # get new url
    new_url = resp.location
    if not new_url:
        raise ValueError('no new location in redirect')
    new_url = URL(resp.location.to_bytes())
    if not new_url.is_absolute:
        new_url.scheme = rreq.host_url.scheme
        new_url.host = rreq.host_url.host
        new_url.port = rreq.host_url.port
    print rreq.host_url, status_code, '->', new_method, new_url

    # build new request with remaining headers
    # (usually this would include filtering cookies and auth)
    new_req = Request(method=new_method, url=new_url)
    print new_req.to_raw_request()

    return client_resp.client.request(request=new_req, timeout=timeout)
Пример #2
0
def follow_next_redirect(client_resp, timeout=TIMEOUT):
    rreq = client_resp.raw_request
    resp = client_resp.response
    status_code = resp.status_code

    if not is_supported_redirect(status_code):
        raise ValueError('not followable')

    # get new method
    if status_code in (301, 302, 303):
        new_method = 'HEAD' if rreq.method == 'HEAD' else 'GET'
    elif status_code in (307, 308):
        raise ValueError()

    # get new url
    new_url = resp.location
    if not new_url:
        raise ValueError('no new location in redirect')
    new_url = URL(resp.location.to_bytes())
    if not new_url.is_absolute:
        new_url.scheme = rreq.host_url.scheme
        new_url.host = rreq.host_url.host
        new_url.port = rreq.host_url.port
    print rreq.host_url, status_code, '->', new_method, new_url

    # build new request with remaining headers
    # (usually this would include filtering cookies and auth)
    new_req = Request(method=new_method, url=new_url)
    print new_req.to_raw_request()

    return client_resp.client.request(request=new_req, timeout=timeout)
Пример #3
0
def test_post(url, method='POST'):
    client = Client()
    data = '{}'  # 'a' * 2 ** 22
    req = Request(method, url, body=data)
    req.content_type = 'application/json'
    client_resp = client.request(request=req)
    import pdb
    pdb.set_trace()
Пример #4
0
def main(url, number, output, do_pdb=False):
    client = Client(profile=HematiteProfile())
    # req = Request('GET', 'http://makuro.org/')
    #req = Request('GET', 'http://hatnote.com/')
    # req = Request('GET', 'http://blog.hatnote.com/')

    req = Request('GET', url)
    kwargs = dict(request=req, autoload_body=False, async=True)
    resp_list = [client.request(**kwargs) for i in range(number)]
    resp = resp_list[0]

    join(resp_list, timeout=5.0)

    print resp.raw_response
    print[r.raw_response.status_code for r in resp_list]
    resp.autoload_body = True
    # import pdb; pdb.set_trace()
    join(resp_list, timeout=1.0)
    print resp.raw_response.body
    if output == '-':
        print resp.raw_response.body.data
    elif output:
        with open(output, 'w') as f:
            f.write(resp.raw_response.body.data)
    if do_pdb:
        import pdb
        pdb.set_trace()
Пример #5
0
def test_wikipedia_req():
    req = Request.from_bytes(WP_REQ)
    assert req.connection == 'keep-alive'
    re_req = req.to_bytes()
    assert _cmpable_req(re_req) == _cmpable_req(WP_REQ)
Пример #6
0
def test_basic_req():
    req = Request.from_bytes(BASIC_REQ)
    assert req.to_bytes() == BASIC_REQ
Пример #7
0
def test_basic_req_url():
    req = Request.from_bytes(BASIC_REQ)
    assert req.url.encode() == 'http://tooxols.ietf.org/html/rfc3986'
Пример #8
0
 def async (self, url, body=None, nonblocking=True):
     req = Request(self.method, url, body=body)
     self.client.populate_headers(req)
     return self.client.request(request=req, async=True)
Пример #9
0
 def __call__(self, url, body=None, timeout=DEFAULT_TIMEOUT):
     req = Request(self.method, url, body=body)
     self.client.populate_headers(req)
     return self.client.request(request=req, timeout=timeout)
Пример #10
0
def test_wikipedia_req():
    req = Request.from_bytes(WP_REQ)
    assert req.connection == 'keep-alive'
    re_req = req.to_bytes()
    assert _cmpable_req(re_req) == _cmpable_req(WP_REQ)
Пример #11
0
def test_basic_req():
    req = Request.from_bytes(BASIC_REQ)
    assert req.to_bytes() == BASIC_REQ
Пример #12
0
def test_basic_req_url():
    req = Request.from_bytes(BASIC_REQ)
    assert req.url.encode() == 'http://tooxols.ietf.org/html/rfc3986'