2017-07-26 134 views
0

我試圖在需要用戶名和密碼的Python中打開URL。 在一開始,有出這樣的錯誤:AbstractBasicAuthHandler不支持以下方案:'Negotiate'[Python]

hereurllib.error.HTTPError: HTTP Error 401: Unauthorized 

以下教程我改裝我的腳本是這樣的:

import urllib.request 
url = 'http://example.com' 
username = 'user' 
password = 'password' 
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() 
passman.add_password(None, url, username, password) 
auth_handler = urllib.request.HTTPBasicAuthHandler(passman) 
opener = urllib.request.build_opener(auth_handler) 
urllib.request.install_opener(opener) 

後,錯誤消失了,但另一個出現在它的地方:

ValueError: AbstractBasicAuthHandler does not support the following scheme: 'Negotiate' 

任何人都知道可能是什麼問題?

+0

我不認爲我很明白。我已經把它寫在第七行了。 – Zombistic

回答

0

切換到requests

Basic Authentication

Many web services that require authentication accept HTTP Basic Auth. This is the simplest kind, and Requests supports it straight out of the box.

Making requests with HTTP Basic Auth is very simple:

import requests 
from requests.auth import HTTPBasicAuth 
requests.get('http://example.com', auth=HTTPBasicAuth('user', 'pass')) 
<Response [200]> 

In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand for using it:

requests.get('https://api.github.com/user', auth=('user', 'pass')) 
<Response [200]> 

Providing the credentials in a tuple like this is exactly the same as the HTTPBasicAuth example above.

+0

我確實使用過它。這已經顯示出來了:'TypeError:add_password()缺少1所需的位置參數:'realm''所以我在add_password結尾加了右括號。仍然沒有工作(顯示另一個錯誤),但這次很可能是我的網址有問題。 – Zombistic

+1

現在工作得很好。謝謝,我很感激。 – Zombistic

相關問題