天天看點

python requests通路報403 "Invalid username/password."

使用python requests通路一個rest api時,總是報錯:

換成curl通路就沒有問題。

為什麼會這樣呢?

把requests的請求header print出啦,看到加了authorization,但是代碼并沒有使用任何認證。

猜測和機器環境有關系。使用者認證資訊應該是存在某個檔案中的吧?

用strace看一下打開了哪些檔案

通路了一個可疑檔案 ~/.netrc

把這個檔案拿掉,接口通路就沒有報錯了。

查一下requests的文檔,requests果然會讀取.netrc檔案

<a href="http://docs.python-requests.org/en/master/user/authentication/?highlight=netrc">http://docs.python-requests.org/en/master/user/authentication/?highlight=netrc</a>