Python 请求:IOError:[Errno 22] 无效参数
Posted
技术标签:
【中文标题】Python 请求:IOError:[Errno 22] 无效参数【英文标题】:Python Requests: IOError: [Errno 22] Invalid argument 【发布时间】:2016-05-29 18:14:50 【问题描述】:我是 Python 请求的新手,在尝试 requests.get()
时遇到了 IOError:[Errno 22] Invalid argument
。简而言之,我正在尝试使用 SSL 连接到内部 Web 应用程序,因此我根据请求文档传递了证书/密钥组合。
我花了很多时间研究潜在问题,并看到一些关于潜在 SNI 问题的提及,但我不够精明,无法知道如何解决该问题(同样,对于 Requests 来说是新手)。欣赏任何朝着正确方向/进一步挖掘的地方(我猜是 urllib3 部分?)
我的代码
import requests
cert_file_path = "/Users/me/Documents/cert.pem"
key_file_path = "/Users/me/Documents/key.pem"
url = "https://mydomain/path/to/something"
cert = (cert_file_path, key_file_path)
r = requests.get(url, cert=cert)
我的错误
IOError Traceback (most recent call last)
<ipython-input-48-1ee4a7f23d00> in <module>()
4 url = "https://mydomain/path/to/something"
5 cert = (cert_file_path, key_file_path)
----> 6 r = requests.get(url, cert=cert)
/Users/me/anaconda/lib/python2.7/site-packages/requests/api.pyc in get(url, **kwargs)
66
67 kwargs.setdefault('allow_redirects', True)
---> 68 return request('get', url, **kwargs)
69
70
/Users/me/anaconda/lib/python2.7/site-packages/requests/api.pyc in request(method, url, **kwargs)
48
49 session = sessions.Session()
---> 50 response = session.request(method=method, url=url, **kwargs)
51 # By explicitly closing the session, we avoid leaving sockets open which
52 # can trigger a ResourceWarning in some cases, and look like a memory leak
/Users/me/anaconda/lib/python2.7/site-packages/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
462
463 send_kwargs.update(settings)
--> 464 resp = self.send(prep, **send_kwargs)
465
466 return resp
/Users/me/anaconda/lib/python2.7/site-packages/requests/sessions.pyc in send(self, request, **kwargs)
574
575 # Send the request
--> 576 r = adapter.send(request, **kwargs)
577
578 # Total elapsed time of the request (approximately)
/Users/me/anaconda/lib/python2.7/site-packages/requests/adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
368 decode_content=False,
369 retries=self.max_retries,
--> 370 timeout=timeout
371 )
372
/Users/me/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, **response_kw)
542 httplib_response = self._make_request(conn, method, url,
543 timeout=timeout_obj,
--> 544 body=body, headers=headers)
545
546 # If we're going to release the connection in ``finally:``, then
/Users/me/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in _make_request(self, conn, method, url, timeout, **httplib_request_kw)
339 # Trigger any extra validation we need to do.
340 try:
--> 341 self._validate_conn(conn)
342 except (SocketTimeout, BaseSSLError) as e:
343 # Py2 raises this as a BaseSSLError, Py3 raises it as socket timeout.
/Users/me/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in _validate_conn(self, conn)
760 # Force connect early to allow us to validate the connection.
761 if not getattr(conn, 'sock', None): # AppEngine might not have `.sock`
--> 762 conn.connect()
763
764 if not conn.is_verified:
/Users/me/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connection.pyc in connect(self)
236 ca_certs=self.ca_certs,
237 server_hostname=hostname,
--> 238 ssl_version=resolved_ssl_version)
239
240 if self.assert_fingerprint:
/Users/me/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.pyc in ssl_wrap_socket(sock, keyfile, certfile, cert_reqs, ca_certs, server_hostname, ssl_version, ciphers, ssl_context)
261 raise
262 if certfile:
--> 263 context.load_cert_chain(certfile, keyfile)
264 if HAS_SNI: # Platform-specific: OpenSSL with enabled SNI
265 return context.wrap_socket(sock, server_hostname=server_hostname)
IOError: [Errno 22] Invalid argument
环境
Python:Python 2.7.11 :: Anaconda 2.4.1 (x86_64)请求:2.6.0Mac OSX em>:优胜美地 (10.10.5)
【问题讨论】:
您确定证书和密钥文件路径是否正确,并且具有适当的权限以供程序读取? @JohnGordon 路径绝对正确(当我有错字时,我在早期版本中得到了类似于“无效路径”的内容)。至于是否可以读取证书/密钥,我很想通过一种不同的方法进行检查,以验证是否有一个我可以使用。在以前的迭代中,在我得到我需要的证书/密钥文件之前,它会抛出类似于“证书/密钥不匹配”或“无效 ssl”的东西 您是直接以自己的身份运行该程序,还是在其他环境中执行(例如在 Web 服务器中)?如果是后者,请确保文件在该上下文中是可读的。 作为一个简单的检查,您可以编写一个非常短的脚本来尝试打开这两个文件,看看它是否得到相同的错误。 @JohnGordon 以我自己的身份运行它(在同一个盒子上,我点击浏览器没有任何问题);两个文件都可以从 bash/终端打开 【参考方案1】:我认为您需要安装请求模块,安装后,您可以使用:
request.get(url)
【讨论】:
以上是关于Python 请求:IOError:[Errno 22] 无效参数的主要内容,如果未能解决你的问题,请参考以下文章
Django:IOError [Errno 2] 使用 python 读取 JSON 文件路径时没有这样的文件或目录
Python IOError 中的错误:[Errno 2] 没有这样的文件或目录:'data.csv' [重复]
Python IOError: [Errno 22] invalid mode ('r') 解决方法