[英]Getting ('Connection aborted.', OSError(0, 'Error') errors with python requests
I'm trying to write code to login to a website.我正在尝试编写代码以登录网站。
First I tested using ARC .首先,我使用ARC进行了测试。 It works fine.
它工作正常。 Image
图片
So I wrote python code like this:所以我写了这样的python代码:
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.只有当我运行Fiddler 时,代码才能正常工作。 I don't know the reason.
我不知道原因。
Even though I tried using C language, I still got an error on this website.即使我尝试使用 C 语言,我仍然在这个网站上出现错误。
Can you solve this problem using only Python?你能只用 Python 解决这个问题吗?
I solved the problem using OPENSSL 1.0.1f
.我使用 OPENSSL
1.0.1f
解决了这个问题。
The domain only supported TLS 1.0, SSLv2, and SSLv3.该域仅支持 TLS 1.0、SSLv2 和 SSLv3。 These protocols are outdated and rarely used.
这些协议已经过时且很少使用。 So I doubted the compatibility of OPENSSL
1.1.1
series.所以怀疑OPENSSL
1.1.1
系列的兼容性。 As a result, using openssl version 1.0.1f
solved the problem.结果,使用openssl
1.0.1f
版本解决了这个问题。 However, using older openssl in Python may require extra work.但是,在 Python 中使用较旧的 openssl 可能需要额外的工作。
I'm not very good at English, but I hope this helps.我的英语不是很好,但我希望这会有所帮助。
Have you tried using a try/except?您是否尝试过使用 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!有趣的是,我发现只有在响应为 200 时才输入“如果响应” - 我不知道为什么但我没有抱怨,哈哈!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.