我正在使用urllib3来对付具有自签名证书的私有服务.有没有办法让urllib3忽略证书错误并提出请求呢?
import urllib3
c = urllib3.HTTPSConnectionPool('10.0.3.168', port=9001)
c.request('GET', '/')
Run Code Online (Sandbox Code Playgroud)
使用以下内容时:
import urllib3
c = urllib3.HTTPSConnectionPool('10.0.3.168', port=9001, cert_reqs='CERT_NONE')
c.request('GET', '/')
Run Code Online (Sandbox Code Playgroud)
引发以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 67, in request
**urlopen_kw)
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 80, in request_encode_url
return self.urlopen(method, url, **urlopen_kw)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 415, in urlopen
body=body, headers=headers)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 267, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.3/http/client.py", line 1061, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.3/http/client.py", line 1099, in …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用SNI从我在Google App Engine上托管的网站下载HTTPS页面.无论我使用什么库,我都会收到以下错误:
[Errno 8] _ssl.c:504: EOF occurred in violation of protocol
Run Code Online (Sandbox Code Playgroud)
我尝试过多种方式解决错误,包括使用urllib3 openssl monkeypatch:
from urllib3.contrib import pyopenssl
pyopenssl.inject_into_urllib3
Run Code Online (Sandbox Code Playgroud)
但我总是得到上面提到的同样的错误.
有任何想法吗?
我尝试在Google App Engine中获取一些推文并对该推文进行一些分析.
由于urllib3中的一些问题,我面临以下错误:
AttributeError: 'NoneType' object has no attribute 'wrap_socket'
Run Code Online (Sandbox Code Playgroud)
最后三个电话是:
File "/Users/krishna/Documents/DATASCI/twitterapi/analyzetweets/requests/packages/urllib3/connectionpool.py", line 304, in _make_request
self._validate_conn(conn)
File "/Users/krishna/Documents/DATASCI/twitterapi/analyzetweets/requests/packages/urllib3/connectionpool.py", line 722, in _validate_conn
conn.connect()
File "/Users/krishna/Documents/DATASCI/twitterapi/analyzetweets/requests/packages/urllib3/connection.py", line 164, in connect
self.sock = ssl.wrap_socket(conn, self.key_file, self.cert_file)
AttributeError: 'NoneType' object has no attribute 'wrap_socket'
Run Code Online (Sandbox Code Playgroud)
Traceback(最近一次调用最后一次):
INFO 2014-08-24 10:37:05,800 connectionpool.py:695] Starting new HTTPS connection (1): api.twitter.com
ERROR 2014-08-24 10:37:06,175 webapp2.py:1528] 'NoneType' object has no attribute 'wrap_socket'
Traceback (most recent call last):
File "/Users/krishna/google-cloud-sdk/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, …
Run Code Online (Sandbox Code Playgroud) twitter google-app-engine urllib3 twitter-oauth python-requests
更新用户代理信息的正确方法是什么urllib3
?
如何检查用户代理信息是否确实已更改并正在使用?
例如:
user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'}
http = urllib3.PoolManager(10, headers=user_agent)
r1 = http.request('GET', 'http://example.com/')
if r1.status is 200:
with open('somefile','w+') as f:
f.write(r1.data)
Run Code Online (Sandbox Code Playgroud)
当我创建一个PoolManager
at时,http
我查看了它,dir(http)
并且http.headers
默认情况下看到它是空的并更新为指定的用户代理信息,但它是否正在使用?无论如何都要检查而不必查看apache
日志?
实际上/var/log/apache2/access.log
在尝试更新用户代理后进行检查:
>>> import urllib3
>>> user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'}
>>> http = urllib3.PoolManager(2, headers=user_agent)
>>> r = http.request('GET','localhost')
>>> with open('/var/log/apache2/access.log','r') as f:
... last_line = f.readlines()[-1]
...
>>> …
Run Code Online (Sandbox Code Playgroud) 我在openstack Designate的本地设置上进行代码修改.
实际上它之前工作正常.
但是现在我正在尝试执行之前工作正常的相同命令.
不幸的是现在对于同一个命令,我收到的错误如下:
root@newds:~# designate record-list 5e18999d-1b4c-43f9-94e8-2bb2aab46aa0
ERROR: HTTPConnectionPool(host='127.0.0.1', port=9001): Max retries exceeded with url: /v1//domains/5e18999d-1b4c-43f9-94e8-2bb2aab46aa0/records (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f2077e2c210>: Failed to establish a new connection: [Errno 111] Connection refused',))
Run Code Online (Sandbox Code Playgroud)
有人帮我解决了这个问题.
我正在使用python 3.5,我正在检查urllib模块Vs请求模块的性能.我在python中编写了两个客户端,第一个使用urllib模块,第二个使用请求模块.它们都生成二进制数据,我将其发送到基于烧瓶的服务器,并从烧瓶服务器发送二进制数据到客户端.我发现从客户端向服务器发送数据的时间花了两个时间用于两个模块(urllib,请求),但是从urllib到客户端返回数据所花费的时间比urllib要快两倍.我正在使用localhost.
我的问题是为什么?
我对请求模块做错了什么让它变得更慢?
这是服务器代码:
from flask import Flask, request
app = Flask(__name__)
from timeit import default_timer as timer
import os
@app.route('/onStringSend', methods=['GET', 'POST'])
def onStringSend():
return data
if __name__ == '__main__':
data_size = int(1e7)
data = os.urandom(data_size)
app.run(host="0.0.0.0", port=8080)
Run Code Online (Sandbox Code Playgroud)
这是基于urllib的客户端代码:
import urllib.request as urllib2
import urllib.parse
from timeit import default_timer as timer
import os
data_size = int(1e7)
num_of_runs = 20
url = 'http://127.0.0.1:8080/onStringSend'
def send_binary_data():
data = os.urandom(data_size)
headers = {'User-Agent': 'Mozilla/5.0 (compatible; Chrome/22.0.1229.94; Windows NT)', 'Content-Length': '%d' % …
Run Code Online (Sandbox Code Playgroud) 通过带有Python的Cloud Vision API客户端运行标签检测时,遇到“打开文件过多”错误。
当我在发布本文之前在GitHub上询问此问题时,维护人员向我提供了一个建议,该问题是一般的Python问题而不是API。
根据这个建议,我还不明白为什么Python会抛出“太多打开的文件”。
我进行了日志记录,结果表明urllib3引发了此类错误,尽管我没有明确导入该软件包。
我怎么了 请帮我。
我的环境是
错误日志:
[2018-05-25 20:18:46,573] {label_detection.py:60} DEBUG - success open decile_data/image/src/00000814.jpg
[2018-05-25 20:18:46,573] {label_detection.py:62} DEBUG - success convert image to types.Image
[2018-05-25 20:18:46,657] {requests.py:117} DEBUG - Making request: POST https://accounts.google.com/o/oauth2/token
[2018-05-25 20:18:46,657] {connectionpool.py:824} DEBUG - Starting new HTTPS connection (1): accounts.google.com
[2018-05-25 20:18:46,775] {connectionpool.py:396} DEBUG - https://accounts.google.com:443 "POST /o/oauth2/token HTTP/1.1" 200 None
[2018-05-25 20:18:47,803] {label_detection.py:60} DEBUG - success open decile_data/image/src/00000815.jpg
[2018-05-25 20:18:47,803] {label_detection.py:62} DEBUG …
Run Code Online (Sandbox Code Playgroud) 如何抑制出现在 urllib3 库中的Failed to parse headers 错误?
以下错误不断出现:
Failed to parse headers (url=https://test): [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Traceback (most recent call last):
File "/opt/test_project/.venv/lib/python3.5/site-packages/urllib3/connectionpool.py", line 399, in _make_request
assert_header_parsing(httplib_response.msg)
File "/opt/test_project/.venv/lib/python3.5/site-packages/urllib3/util/response.py", line 66, in assert_header_parsing
raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
urllib3.exceptions.HeaderParsingError: [StartBoundaryNotFoundDefect(), MultipartInvariantViolationDefect()], unparsed data: ''
Run Code Online (Sandbox Code Playgroud)
我试图用
import urllib3
# Disable SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Disbale urllib3.exceptions.HeaderParsingError:
urllib3.disable_warnings(urllib3.exceptions.HeaderParsingError)
Run Code Online (Sandbox Code Playgroud)
但似乎它不起作用,因为它仍在出现。
网上有一些解决方案,但这是通过日志抑制的。我正在寻找一种方法来抑制它而不是从日志级别。
AFAIK,这只是来自 urllib3 的警告,它已被报告为错误。因此,有什么办法可以抑制这种情况吗?
我正在尝试将代码部署到我没有 root 访问权限的服务器。所以我想的解决方案是使用pyinstaller和staticx进行部署。
我的代码在 python 3.7 中运行,简而言之,执行以下操作:
import requests
response = requests.get('http://example.com/api/action')
# Do something with the response
Run Code Online (Sandbox Code Playgroud)
当我在我的环境中运行它时,即使在使用pyinstaller
和“构建”之后staticx
,它也能完美运行。
但是,当我尝试部署它时(服务器运行的是 Red Hat Enterprise Linux Server 7.4 版,它没有 python 3),我得到:
Traceback (most recent call last):
File "site-packages/urllib3/connection.py", line 160, in _new_conn
File "site-packages/urllib3/util/connection.py", line 57, in create_connection
File "socket.py", line 748, in getaddrinfo
socket.gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent …
Run Code Online (Sandbox Code Playgroud) 有没有办法告诉 Python urllib3 在一段时间后不要重用空闲连接,而是关闭它们?
查看https://urllib3.readthedocs.io/en/latest/reference/index.html#module-urllib3.connectionpool似乎没有显示任何相关内容。
urllib3 ×10
python ×9
python-3.x ×2
dns ×1
http ×1
keep-alive ×1
openstack ×1
performance ×1
sni ×1
tcp ×1
twitter ×1
ubuntu ×1
urllib ×1
urllib2 ×1