python 使用 urllib2 HTTPS 登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1045886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:21:14  来源:igfitidea点击:

HTTPS log in with urllib2

pythonauthenticationhttpsurllib2

提问by Parker Coates

I currently have a little script that downloads a webpage and extracts some data I'm interested in. Nothing fancy.

我目前有一个小脚本,可以下载网页并提取一些我感兴趣的数据。没什么特别的。

Currently I'm downloading the page like so:

目前我正在像这样下载页面:

import commands
command = 'wget --output-document=- --quiet --http-user=USER --http-password=PASSWORD https://www.example.ca/page.aspx'
status, text = commands.getstatusoutput(command)

Although this works perfectly, I thought it'd make sense to remove the dependency on wget. I thought it should be trivial to convert the above to urllib2, but thus far I've had zero success. The Internet is full urllib2 examples, but I haven't found anything that matches my need for simple username and password HTTP authentication with a HTTPS server.

虽然这很有效,但我认为消除对 wget 的依赖是有意义的。我认为将上述内容转换为 urllib2 应该很简单,但到目前为止我的成功率为零。互联网上有完整的 urllib2 示例,但我没有找到任何符合我对使用 HTTPS 服务器进行简单用户名和密码 HTTP 身份验证的需求的内容。

采纳答案by Weston

The requestsmodule provides a modern API to HTTP/HTTPS capabilities.

请求模块提供了一个现代化的API到HTTP / HTTPS的能力。

import requests

url = 'https://www.someserver.com/toplevelurl/somepage.htm'

res = requests.get(url, auth=('USER', 'PASSWORD'))

status = res.status_code
text   = res.text

回答by Boldewyn

thissays, it should be straight forward

说,它应该是直截了当的

[as] long as your local Python has SSL support.

[只要] 本地 Python 支持 SSL。

If you use just HTTP Basic Authentication, you must set different handler, as described here.

如果你只使用HTTP基本身份验证,则必须设置不同的处理程序,如所描述这里

Quoting the example there:

引用那里的例子:

import urllib2

theurl = 'http://www.someserver.com/toplevelurl/somepage.htm'
username = 'johnny'
password = 'XXXXXX'
# a great password

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager
passman.add_password(None, theurl, username, password)
# because we have put None at the start it will always
# use this username/password combination for  urls
# for which `theurl` is a super-url

authhandler = urllib2.HTTPBasicAuthHandler(passman)
# create the AuthHandler

opener = urllib2.build_opener(authhandler)

urllib2.install_opener(opener)
# All calls to urllib2.urlopen will now use our handler
# Make sure not to include the protocol in with the URL, or
# HTTPPasswordMgrWithDefaultRealm will be very confused.
# You must (of course) use it when fetching the page though.

pagehandle = urllib2.urlopen(theurl)
# authentication is now handled automatically for us

If you do Digest, you'll have to set some additional headers, but they are the same regardless of SSL usage. Googlefor python+urllib2+http+digest.

如果您执行 Digest,则必须设置一些额外的标头,但无论 SSL 使用情况如何,它们都是相同的。谷歌搜索 python+urllib2+http+digest。

Cheers,

干杯,

回答by Corey Goldberg

The urllib2 documentation has an example of working with Basic Authentication:

urllib2 文档有一个使用基本身份验证的示例:

http://docs.python.org/library/urllib2.html#examples

http://docs.python.org/library/urllib2.html#examples