PythonAnywhere запрашивает сеанс, вызывающий исключение «соединение прервано» - PullRequest
0 голосов
/ 04 марта 2020

Я пытаюсь запустить свой скрипт для PA, и хотя он работает локально, он не работает на PA, и я не могу понять, почему. У меня есть платная версия PA, поэтому я могу получить доступ к этому сайту, который не находится в белом списке. Вот мой сценарий:

with requests.Session() as session:
    # initates the login session
    if os.path.exists(document_report + '.pdf'):
        # initates the login session
        post = session.post(POST_LOGIN_URL, data=payload, headers = dict(referrer = 'https://www.example.com/loginpage'))

        #This URL is the page you actually want to pull down with requests.
        REQUEST_URL = f'https://www.example.com/{stuff}/{document}'
        #gets the request URL
        r = session.get(REQUEST_URL)
        # saves to directory


        with open('./digests/1.pdf', 'wb') as j:
            j.write(r.content)
            j.close()



    else:
        post = session.post(POST_LOGIN_URL, data=payload, headers = dict(referrer = 'https://www.example.com/loginpage'))

        #This URL is the page you actually want to pull down with requests.
        REQUEST_URL = f'https://www.example.com/{stuff}/{document}'
        #gets the request URL
        r = session.get(REQUEST_URL)
        # saves to directory


        with open(document_report + '.pdf', 'wb') as j:
            j.write(r.content)
            j.close()

Когда я запускаю код, я получаю следующее сообщение:

Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.7/http/client.py", line 1344, in getresponse
    response.begin()
  File "/usr/lib/python3.7/http/client.py", line 306, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.7/http/client.py", line 275, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/usr/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
        timeout=timeout
      File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen
        _stacktrace=sys.exc_info()[2])
      File "/usr/lib/python3.7/site-packages/urllib3/util/retry.py", line 368, in increment
        raise six.reraise(type(error), error, _stacktrace)
      File "/usr/lib/python3.7/site-packages/urllib3/packages/six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
        chunked=chunked)
      File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 384, in _make_request
        six.raise_from(e, None)
      File "<string>", line 2, in raise_from
      File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 380, in _make_request
        httplib_response = conn.getresponse()
      File "/usr/lib/python3.7/http/client.py", line 1344, in getresponse
        response.begin()
      File "/usr/lib/python3.7/http/client.py", line 306, in begin
        version, status, reason = self._read_status()
      File "/usr/lib/python3.7/http/client.py", line 275, in _read_status
        raise RemoteDisconnected("Remote end closed connection without"
    urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "/home/legalaide/emailProcessorV2.py", line 567, in <module>
        post = session.post(POST_LOGIN_URL, data=payload, headers = dict(referrer = 'https://www.example.com/loginpage'))
      File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 581, in post
        return self.request('POST', url, data=data, json=json, **kwargs)
      File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
        resp = self.send(prep, **send_kwargs)
      File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
        r = adapter.send(request, **kwargs)
      File "/usr/lib/python3.7/site-packages/requests/adapters.py", line 498, in send
        raise ConnectionError(err, request=request)
    requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

Я попробовал еще раз локально, и он все еще работает. Любое понимание решения будет удивительным!

...