Getting ('Connection aborted.', OSError(0, 'Error') errors with python requests
Asked Answered
D

3

25

I'm trying to write code to login to a website.
First I tested using ARC. It works fine. Image

So I wrote python code like this:

import requests


url = 'https://www.bible.ac.kr/login.aspx/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko',
    'Referer': 'http://www.bible.ac.kr/'
}


req = requests.get(url, headers=headers) # OR requests.get(url, headers=headers, verify=False)

print(req.status_code)

but I got errors.

Error

Traceback (most recent call last):
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 344, in _make_request
    self._validate_conn(conn)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 843, in _validate_conn
    conn.connect()
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connection.py", line 370, in connect
    ssl_context=context)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 355, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
OSError: [Errno 0] Error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/util/retry.py", line 368, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 344, in _make_request
    self._validate_conn(conn)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 843, in _validate_conn
    conn.connect()
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connection.py", line 370, in connect
    ssl_context=context)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 355, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(0, 'Error'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kyungmin/PycharmProjects/untitled14/a.py", line 11, in <module>
    req = requests.get(url, headers=headers)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError(0, 'Error'))

Process finished with exit code 1

I have not solved this problem for a long time.
Only when I run Fiddler, the code works fine. I don't know the reason.
Even though I tried using C language, I still got an error on this website.
Can you solve this problem using only Python?

Dumah answered 27/8, 2019 at 9:51 Comment(3)
i am facing the same problem, with homebrew installed python3.7 in mac os.Sennit
It looks like the url does not work.Stipitate
@Dumah - is this still relevant? I am getting The requested URL /login.aspx was not found on this server. when trying to access the page using my browserGraphics
D
1

I solved the problem using OPENSSL 1.0.1f.

The domain only supported TLS 1.0, SSLv2, and SSLv3. These protocols are outdated and rarely used. So I doubted the compatibility of OPENSSL 1.1.1 series. As a result, using openssl version 1.0.1f solved the problem. However, using older openssl in Python may require extra work.

I'm not very good at English, but I hope this helps.

Dumah answered 19/7, 2020 at 7:51 Comment(3)
Can you please post the code how to use OpenSSL with requests module?Hollingshead
I also would like to know how to use OpenSSL in requests... =/Ariew
Cannot do it with python3.8. Openssl 1.0 series is not compatible with python3.8 it seems.Scopophilia
D
0

The wrap_socket call was not from a SSLContext; this seemed to cause problem when making SSL handshake; in urllib3 1.26 it was changed to context.wrap_socket.

Installing a new version of Python can solve such problems.

Depressant answered 22/4, 2022 at 15:25 Comment(0)
W
-1

Have you tried using a try/except?

while statuscode != 200:
    response = 0
    try:
        response = requests.get(url, params, timeout=7)
    except (
        requests.ConnectionError,
        requests.exceptions.ReadTimeout,
        requests.exceptions.Timeout,
        requests.exceptions.ConnectTimeout,
    ) as e:
        statuscode = 0
        print(e)
    if response:
        statuscode = response.status_code

I have interestingly found that "if response" is only entered when the response is 200 - I don't know why but I am not complaining, haha!

Warranty answered 16/7, 2020 at 19:58 Comment(1)
Welcome to Stack Overflow community. As you're providing some code example, it is a good practice to format the code, here are some details about code formatting: meta.#251861Groome

© 2022 - 2024 — McMap. All rights reserved.