我想使用 python 登录一个使用 的网站Microsoft Forefront
,并检索内部网页的内容进行处理。
我对 python 并不陌生,但我没有使用过任何 URL 库。我检查了以下帖子:
我还尝试了几个模块,例如requests
. 我仍然无法理解应该如何完成,输入用户名/密码是否足够?或者我应该以某种方式使用 cookie 进行身份验证?任何示例代码都将不胜感激。
这是我到目前为止的代码:
import requests
NAME = 'XXX'
PASSWORD = 'XXX'
URL = 'https://intra.xxx.se/CookieAuth.dll?GetLogon?curl=Z2F&reason=0&formdir=3'
def main():
# Start a session so we can have persistant cookies
session = requests.session()
# This is the form data that the page sends when logging in
login_data = {
'username': NAME,
'password': PASSWORD,
'SubmitCreds': 'login',
}
# Authenticate
r = session.post(URL, data=login_data)
# Try accessing a page that requires you to be logged in
r = session.get('https://intra.xxx.se/?t=1-2')
print r
main()
session.post
但是上面的代码在-line上导致以下异常:
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='intra.xxx.se', port=443): Max retries exceeded with url: /CookieAuth.dll?GetLogon?curl=Z2F&reason=0&formdir=3 (Caused by <class 'socket.error'>: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
更新: 我注意到我提供了错误的用户名/密码。更新后,我会收到带有上述代码的 HTTP-200 响应,但是当我尝试访问任何内部站点时,我会收到 HTTP 401 响应。为什么会这样?上面的代码有什么问题?我应该以某种方式使用cookies吗?