我想你正試圖通過基本認證。在這種情況下,你可以這樣處理:
import urllib2
username = 'user1'
password = '123456'
#This should be the base url you wanted to access.
baseurl = 'http://server_name.com'
#Create a password manager
manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, baseurl, username, password)
#Create an authentication handler using the password manager
auth = urllib2.HTTPBasicAuthHandler(manager)
#Create an opener that will replace the default urlopen method on further calls
opener = urllib2.build_opener(auth)
urllib2.install_opener(opener)
#Here you should access the full url you wanted to open
response = urllib2.urlopen(baseurl + "/file")
服務器如何期待他們?轉到網址並在開發工具中打開網絡選項卡登錄並查看請求的樣子 – IanAuld
請參閱https://docs.python.org/2/howto/urllib2.html#id6 – Selcuk