【问题标题】:Why does my python request to http runs, but not to https?为什么我对 http 的 python 请求运行,而不是对 https?
【发布时间】:2020-04-02 13:19:37
【问题描述】:

我需要连接到这个 uri:uri = 'https://www.wfs.nrw.de/geobasis/wfs_nw_inspire-flurstuecke_alkis?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&STOREDQUERY_ID=urn:ogc:def:query:OGC-WFS::GetFeatureById&ID=CadastralParcel_05518904700963______'

因为它不起作用,所以我尝试了这个来找出我的错误:

import requests
import urllib.request

uri = 'http://www.google.de'
uriS = 'https://www.google.de'

myproxies =  urllib.request.getproxies()

try:
    r = requests.get(uri)
    print('HTTP OK')
except:
    print('HTTP NOT OK')
try:
    r = requests.get(uri, proxies = myproxies)
    print('HTTP WITH PROXIES OK')
except:
    print('HTTP WITH PROXIES NOT OK')
try:
    r = requests.get(uriS)
    print('HTTPS OK')
except:
    print('HTTPS NOT OK')
try:
    r = requests.get(uriS, proxies = myproxies)
    print('HTTPS WITH PROXIES OK')
except:
    print('HTTPS WITH PROXIES NOT OK')

这是结果:

HTTP OK
HTTP WITH PROXIES OK
HTTPS NOT OK
HTTPS WITH PROXIES NOT OK

当我在控制台中执行此操作时会发生这种情况:

>>> import requests
>>> uri = 'https://www.google.de'
>>> r = requests.get(uri)

Traceback (most recent call last):
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\urllib3\connectionpool.py", line 594, in urlopen
        self._prepare_proxy(conn)
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\urllib3\connectionpool.py", line 815, in _prepare_proxy
        conn.connect()
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\urllib3\connection.py", line 324, in connect
        self._tunnel()
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\http\client.py", line 911, in _tunnel
        message.strip()))
    OSError: Tunnel connection failed: 407 Proxy Authorization Required

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\requests\adapters.py", line 445, in send
        timeout=timeout
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
        _stacktrace=sys.exc_info()[2])
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\urllib3\util\retry.py", line 398, in increment
        raise MaxRetryError(_pool, url, error or ResponseError(cause))
    urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.google.de', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authorization Required')))

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\code.py", line 90, in runcode
        exec(code, self.locals)
      File "<input>", line 1, in <module>
      File "<string>", line 3, in <module>
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\requests\api.py", line 72, in get
        return request('get', url, params=params, **kwargs)
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\requests\api.py", line 58, in request
        return session.request(method=method, url=url, **kwargs)
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\requests\sessions.py", line 512, in request
        resp = self.send(prep, **send_kwargs)
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\requests\sessions.py", line 622, in send
        r = adapter.send(request, **kwargs)
      File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\site-packages\requests\adapters.py", line 507, in send
        raise ProxyError(e, request=request)
    requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.google.de', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authorization Required')))

【问题讨论】:

标签: python https python-requests


【解决方案1】:

我们解决了问题!

代码是正确的。

我们(我们的网络管理员)已将 uri "https://www.wfs.nrw.de" 放在不需要身份验证的 uri 列表中。

import requests
import urllib.request
myproxies = urllib.request.getproxies()
uri = 'https://www.wfs.nrw.de/geobasis/wfs_nw_inspire-flurstuecke_alkis?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&STOREDQUERY_ID=urn:ogc:def:query:OGC-WFS::GetFeatureById&ID=CadastralParcel_05518904700963______'
r = requests.get(uri, proxies = myproxies)
print(r.text)

【讨论】:

    猜你喜欢
    • 2017-10-30
    • 2019-06-07
    • 2019-06-02
    • 1970-01-01
    • 2023-04-02
    • 2011-08-27
    • 2017-06-18
    相关资源
    最近更新 更多