[英]Why does my python request to http runs, but not to https?
我需要連接到這個 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')))
我們解決了問題!
代碼是正確的。
我們(我們的網絡管理員)已將 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.