python requests请求出现 requests.exceptions.ConnectionError: ('Connection aborted.', OSError(22, 'Invalid argument'))
Traceback (most recent call last):
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 1042, in _validate_conn
conn.connect()
File "C:\python310\lib\site-packages\urllib3\connection.py", line 395, in connect
self.ssl_context = create_urllib3_context(
File "C:\python310\lib\site-packages\urllib3\util\ssl_.py", line 353, in create_urllib3_context
context.keylog_filename = sslkeylogfile
OSError: [Errno 22] Invalid argument: '\u202aD:\\Program Files\\wireshare.log'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\python310\lib\site-packages\requests\adapters.py", line 489, in send
resp = conn.urlopen(
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
retries = retries.increment(
File "C:\python310\lib\site-packages\urllib3\util\retry.py", line 550, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\python310\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
raise value.with_traceback(tb)
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 1042, in _validate_conn
conn.connect()
File "C:\python310\lib\site-packages\urllib3\connection.py", line 395, in connect
self.ssl_context = create_urllib3_context(
File "C:\python310\lib\site-packages\urllib3\util\ssl_.py", line 353, in create_urllib3_context
context.keylog_filename = sslkeylogfile
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(22, 'Invalid argument'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\dve\tesiting_django\mypro\test\asasa.py", line 66, in <module>
res = requests.get(url, headers=headers)
File "C:\python310\lib\site-packages\requests\api.py", line 73, in get
return request("get", url, params=params, **kwargs)
File "C:\python310\lib\site-packages\requests\api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "C:\python310\lib\site-packages\requests\sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
File "C:\python310\lib\site-packages\requests\sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "C:\python310\lib\site-packages\requests\adapters.py", line 547, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError(22, 'Invalid argument'))
进入最后一处报错的代码行数 raise ConnectionError(err, request=request)发现是个捕捉异常的逻辑,在try里打断点
try:
if not chunked:
# conn.urlopen报错代码行
resp = conn.urlopen(
method=request.method,
url=url,
body=request.body,
headers=request.headers,
redirect=False,
assert_same_host=False,
preload_content=False,
decode_content=False,
retries=self.max_retries,
timeout=timeout,
)
# Send the request.
else:
if hasattr(conn, "proxy_pool"):
conn = conn.proxy_pool
low_conn = conn._get_conn(timeout=DEFAULT_POOL_TIMEOUT)
try:
skip_host = "Host" in request.headers
low_conn.putrequest(
request.method,
url,
skip_accept_encoding=True,
skip_host=skip_host,
)
for header, value in request.headers.items():
low_conn.putheader(header, value)
low_conn.endheaders()
for i in request.body:
low_conn.send(hex(len(i))[2:].encode("utf-8"))
low_conn.send(b"\r\n")
low_conn.send(i)
low_conn.send(b"\r\n")
low_conn.send(b"0\r\n\r\n")
# Receive the response from the server
r = low_conn.getresponse()
resp = HTTPResponse.from_httplib(
r,
pool=conn,
connection=low_conn,
preload_content=False,
decode_content=False,
)
except Exception:
# If we hit any problems here, clean up the connection.
# Then, raise so that we can handle the actual exception.
low_conn.close()
raise
except (ProtocolError, OSError) as err:
raise ConnectionError(err, request=request)
发现是这行连接代码报错,抛出的异常,执行代码得出异常报错信息
Traceback (most recent call last):
File "D:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 1, in <module>
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
retries = retries.increment(
File "C:\python310\lib\site-packages\urllib3\util\retry.py", line 550, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\python310\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
raise value.with_traceback(tb)
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "C:\python310\lib\site-packages\urllib3\connectionpool.py", line 1042, in _validate_conn
conn.connect()
File "C:\python310\lib\site-packages\urllib3\connection.py", line 395, in connect
self.ssl_context = create_urllib3_context(
File "C:\python310\lib\site-packages\urllib3\util\ssl_.py", line 353, in create_urllib3_context
context.keylog_filename = sslkeylogfile
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(22, 'Invalid argument'))
进入最后一处报错的代码行数C:\python310\lib\site-packages\urllib3\util\ssl_.py,取消try里的断点,
# Enable logging of TLS session keys via defacto standard environment variable
# 'SSLKEYLOGFILE', if the feature is available (Python 3.8+). Skip empty values.
if hasattr(context, "keylog_filename"):
# 为什么 os.environ.get("SSLKEYLOGFILE")后有异常 '\u202aD:\\Program Files\\wireshare.log'
# TODO
sslkeylogfile = os.environ.get("SSLKEYLOGFILE")
# sslkeylogfile = r"D:\\Program Files\\wireshare.log"
if sslkeylogfile:
context.keylog_filename = sslkeylogfile
发现os.environ.get("SSLKEYLOGFILE")获取的环境变量前面出现\u202aD,去环境变量查看"SSLKEYLOGFILE",发现"SSLKEYLOGFILE"无空格符号等异常
既然 os.environ.get("SSLKEYLOGFILE")指定的是"D:\Program Files\wireshare.log"文件,就修改ssl_.py文件中的源码,写死sslkeylogfile变量的固定值为r"D:\Program Files\wireshare.log"
标签:OSError,22,py,urllib3,File,requests,packages,line,conn From: https://www.cnblogs.com/chron/p/17158711.html