首页 > 编程语言 >python requests请求出现 requests.exceptions.ConnectionError: ('Connection aborted.', OSError(2

python requests请求出现 requests.exceptions.ConnectionError: ('Connection aborted.', OSError(2

时间:2023-02-27 10:12:02浏览次数:59  
标签:OSError 22 py urllib3 File requests packages line conn

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

相关文章

  • 122. 糖果传递(贪心)
    https://www.acwing.com/problem/content/description/124/求最小代价,且数据范围为1e6,大概是O(N)或O(NlogN),大概就是排个序,贪心一般都是排序设定每个小朋友给出的为xi,有如......
  • 一枚Linux驱动开发的2022总结与2023计划
    2022总结2023计划能力与素质1.警惕身边的"奶头乐",追求高级的快乐奶头乐(英语:tittytainment),又译为奶头娱乐或奶嘴娱乐,是一个合成词,来自于英文“titty”(奶头)与“entert......
  • java学习日记20230227-java学习方法/转义字符/注释
    Java学习方法学习java基本原理和基本语法快速入门(基本程序CRUD)研究技术的注意事项,使用细节,使用规范,如何优化JAVA转义字符\t:一个制表位,实现对......
  • java学习日记20230226-java环境搭建及运行机制
    JDK安装配置环境变量:当执行的程序在当前目录不存在时,windows去系统path环境变量里面进行查找,如果没有找到报错不存在该命令。我的电脑-属性-高级系统设置-......
  • Ubuntu 22.04部署小记
    又快到了阿里云的续费时间,由于工作内容的变化,除了rss阅读外,之前大部分使用的服务目前已不再使用,近期好哥们为我免费提供了一台消费级虚拟机用于替代付费云主机,初步评估也能......
  • P8421 [THUPC2022 决赛] rsraogps
    \(\text{Solution}\)肯定扫描线在考虑维护什么东西,假设\(r\)右移时可以暴力得到所有新值,发现需要维护区间历史版本和以及区间当前值之和这三个操作对于一个数来说变化......
  • 2022-SZUACM招新 训练赛2
    2022-SZUACM招新训练赛2https://vjudge.net/contest/544906#overview下午打了一下,稍微记录一波,有一些蛮有意思的小题。A-Arrayhttps://codeforces.com/problemset/p......
  • 03_19_JavaWeb||day22_Ajax&Json||day22_Ajax&Json
    今日内容1.AJAX:增强用户体验2.JSON:一种数据格式1.AJAX:概念:ASynchronousJavaScriptAndXML异步的JavaScript和XML异步和同步:客户端和服务器端相互通信的基础上......
  • Deleaker is ready for Visual Studio 2022!
    DeleakerisreadyforVisualStudio2022!On November3,2021 By ArtemRazin In UncategorizedSinceMicrosoftannouncedthefirstpreviewofVisualStudi......
  • 2022秋学期 高级操作系统期末整理
    2022秋学期高级操作系统期末整理原文C0引言本文内容来自于对高级操作系统课程资料的整理;本文所涉及的课程指东北某沿海高校,计算机学院硕士生必修课“高级操作系统”,......