theelous3 / asks

Async requests-like httplib for python.
MIT License
508 stars 63 forks source link

BadHttpRequest error #119

Closed Giftcarder closed 5 years ago

Giftcarder commented 5 years ago

I keep trying to send a post request to 2captcha (captcha solving service) using asks, but at random I keep getting "BadHttpRequest" errors, now the error only says "the servers returned a invalid http request" or something, which doesn't help me much to debug, so I decided to dig further and it returns this error

"can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE" What does this mean? And how could I avoid it? I tried increasing the timeout time but that didn't help

the code I'm using:

class TwoCaptcha(object):
    def __init__(self, api_key, Session, sleep_time=10):
        self.s = Session
        self.api_key = api_key
        self.sleep_time = sleep_time

    async def __get_captcha_id(self, site_key, page_url):
        params = {
            'googlekey': site_key,
            'pageurl': page_url,
            'method': 'userrecaptcha',
            'key': self.api_key
        }
        endpoint = 'http://2captcha.com/in.php'
        try:
            response = await self.s.post(endpoint, data=params)
            print(response.text)
            captcha_id = response.text.split('|')[1]
            print(captcha_id)
        except Exception as e:
            print("GET CAPTCHA ID ERROR: " + str(e))
            captcha_id = "NULL"
            pass
        return captcha_id

    async def __get_captcha_token(self, captcha_id):
        params = {
            'id': captcha_id,
            'action': 'get',
            'key': self.api_key
        }
        endpoint = 'http://2captcha.com/res.php'
        response = await self.s.get(endpoint, params=params)
        try:
            while 'CAPCHA_NOT_READY' in response.text:
                await trio.sleep(self.sleep_time)
                response = await self.s.get(endpoint, params=params)
                print(response.text)
            captcha_token = response.text.split('|')[1]
            print(captcha_token)

        except Exception as e:
            print("GET CAPTCHA TOKEN ERROR: " + str(e))
            captcha_token = "NULL"
            pass
        return captcha_token

    async def solve_captcha(self, site_key, page_url):
        captcha_id = await self.__get_captcha_id(site_key, page_url)
        captcha_token = await self.__get_captcha_token(captcha_id)
        return captcha_token
async def worker():
    captcha_token = await twoCaptcha.solve_captcha(site_key="http://example", page_url="http://example")
    s.get("http://example.url", params={'reCaptchaResponse':captcha_token}
async def run_task():
    s = Session(connections=10)
    twoCaptcha = TwoCaptcha("api_key", s)
    async with trio.open_nursery() as nursery:
        nursery.start_soon(worker, s)
trio.run(run_task)
Giftcarder commented 5 years ago

Update: The error happens also with a async Anticaptcha library

It seems the issue happens on post requests? Even ignoring it will make it raise somewhere else

Here's a more in detail crash log @theelous3 if you can help?

Traceback (most recent call last):
  File "D:\PYTHON\REDACTED\parser.py", line 160, in check
    trio.run(run_task)
  File "C:\ProgramData\Anaconda3\lib\site-packages\trio\_core\_run.py", line 1444, in run
    raise runner.main_task_outcome.error
  File "D:\PYTHON\REDACTED\parser.py", line 153, in run_task
    await trio.sleep(1)
  File "C:\ProgramData\Anaconda3\lib\site-packages\trio\_core\_run.py", line 506, in __aexit__
    raise combined_error_from_nursery
trio.MultiError: BadHttpResponse("can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE"), SSLError(1, '[SSL: KRB5_S_INIT] application data after close notify (_ssl.c:2592)')

Details of embedded exception 1:

  Traceback (most recent call last):
    File "C:\ProgramData\Anaconda3\lib\site-packages\h11\_state.py", line 249, in _fire_event_triggered_transitions
      new_state = EVENT_TRIGGERED_TRANSITIONS[role][state][event_type]
  KeyError: <class 'h11._events.ConnectionClosed'>

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\sessions.py", line 184, in request
      sock, r = await req_obj.make_request()
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 216, in make_request
      response_obj = await self._request_io(req, req_body, h11_connection)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 253, in _request_io
      response_obj = await self._catch_response(h11_connection)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 534, in _catch_response
      response = await self._recv_event(h11_connection)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 610, in _recv_event
      event = h11_connection.next_event()
    File "C:\ProgramData\Anaconda3\lib\site-packages\h11\_connection.py", line 439, in next_event
      exc._reraise_as_remote_protocol_error()
    File "C:\ProgramData\Anaconda3\lib\site-packages\h11\_util.py", line 72, in _reraise_as_remote_protocol_error
      raise self
    File "C:\ProgramData\Anaconda3\lib\site-packages\h11\_connection.py", line 422, in next_event
      self._process_event(self.their_role, event)
    File "C:\ProgramData\Anaconda3\lib\site-packages\h11\_connection.py", line 238, in _process_event
      self._cstate.process_event(role, type(event), server_switch_event)
    File "C:\ProgramData\Anaconda3\lib\site-packages\h11\_state.py", line 238, in process_event
      self._fire_event_triggered_transitions(role, event_type)
    File "C:\ProgramData\Anaconda3\lib\site-packages\h11\_state.py", line 253, in _fire_event_triggered_transitions
      .format(event_type.__name__, role, self.states[role]))
  h11._util.RemoteProtocolError: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE

  The above exception was the direct cause of the following exception:

  Traceback (most recent call last):
    File "D:\PYTHON\REDACTED\parser.py", line 104, in worker
      .captcha_handler(module.request(), module.site_key)
    File "D:\PYTHON\REDACTED\AntiCaptcha.py", line 161, in captcha_handler
      result_payload=self.result_payload, sleep_time=self.sleep_time
    File "D:\PYTHON\REDACTED\AntiCaptcha.py", line 66, in get_async_result
      resp = await s.post(get_result_url, json=result_payload)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\sessions.py", line 214, in request
      await self._handle_exception(e, sock)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\sessions.py", line 252, in _handle_exception
      raise BadHttpResponse(str(e)) from e
  asks.errors.BadHttpResponse: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE

Details of embedded exception 2:

  Traceback (most recent call last):
    File "C:\ProgramData\Anaconda3\lib\site-packages\anyio\_networking.py", line 92, in recv
      return self._raw_socket.recv(size, flags)
    File "C:\ProgramData\Anaconda3\lib\ssl.py", line 1037, in recv
      return self.read(buflen)
    File "C:\ProgramData\Anaconda3\lib\ssl.py", line 913, in read
      return self._sslobj.read(len)
  ssl.SSLWantReadError: The operation did not complete (read) (_ssl.c:2471)

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\sessions.py", line 184, in request
      sock, r = await req_obj.make_request()
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 216, in make_request
      response_obj = await self._request_io(req, req_body, h11_connection)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 253, in _request_io
      response_obj = await self._catch_response(h11_connection)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 534, in _catch_response
      response = await self._recv_event(h11_connection)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\request_object.py", line 612, in _recv_event
      h11_connection.receive_data(await self.sock.receive_some(10000))
    File "C:\ProgramData\Anaconda3\lib\site-packages\anyio\_networking.py", line 234, in receive_some
      return await self._socket.recv(max_bytes)
    File "C:\ProgramData\Anaconda3\lib\site-packages\anyio\_networking.py", line 94, in recv
      await self._wait_readable()
    File "C:\ProgramData\Anaconda3\lib\site-packages\anyio\_backends\trio.py", line 173, in wait_socket_readable
      await trio.hazmat.wait_socket_readable(sock)
    File "C:\ProgramData\Anaconda3\lib\site-packages\trio\_core\_io_windows.py", line 451, in wait_socket_readable
      await self._wait_socket("read", sock)
    File "C:\ProgramData\Anaconda3\lib\site-packages\trio\_core\_io_windows.py", line 447, in _wait_socket
      await _core.wait_task_rescheduled(abort)
    File "C:\ProgramData\Anaconda3\lib\site-packages\trio\_core\_traps.py", line 166, in wait_task_rescheduled
      return (await _async_yield(WaitTaskRescheduled(abort_func))).unwrap()
    File "C:\ProgramData\Anaconda3\lib\site-packages\outcome\_sync.py", line 111, in unwrap
      raise captured_error
    File "C:\ProgramData\Anaconda3\lib\site-packages\trio\_core\_run.py", line 740, in raise_cancel
      raise Cancelled._init()
  trio.Cancelled

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "D:\PYTHON\REDACTED\parser.py", line 104, in worker
      .captcha_handler(module.request(), module.site_key)
    File "D:\PYTHON\REDACTED\AntiCaptcha.py", line 161, in captcha_handler
      result_payload=self.result_payload, sleep_time=self.sleep_time
    File "D:\PYTHON\REDACTED\AntiCaptcha.py", line 66, in get_async_result
      resp = await s.post(get_result_url, json=result_payload)
    File "C:\ProgramData\Anaconda3\lib\site-packages\asks\sessions.py", line 221, in request
      await sock.close()
    File "C:\ProgramData\Anaconda3\lib\site-packages\anyio\_networking.py", line 215, in close
      await self._socket.unwrap_tls()
    File "C:\ProgramData\Anaconda3\lib\site-packages\anyio\_networking.py", line 184, in unwrap_tls
      self._raw_socket = cast(ssl.SSLSocket, self._raw_socket).unwrap()
    File "C:\ProgramData\Anaconda3\lib\ssl.py", line 1094, in unwrap
      s = self._sslobj.shutdown()
  ssl.SSLError: [SSL: KRB5_S_INIT] application data after close notify (_ssl.c:2592)