Python 处理 URL 的用户名和密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23576970/
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-08-19 03:10:44  来源:igfitidea点击:

Python handling username and password for URL

pythonpasswordsurllib2

提问by Anthony Honciano

Messing with Python and I'm trying to use this https://updates.opendns.com/nic/update?hostname=, when you got to the URL it will prompt a username and password. I've been looking around and I found something about password managers, so I came up with this:

与 Python 混为一谈,我正在尝试使用此https://updates.opendns.com/nic/update?hostname=,当您到达 URL 时,它会提示输入用户名和密码。我一直在环顾四周,发现了一些关于密码管理器的信息,所以我想出了这个:

urll = "http://url.com"
username = "username"
password = "password"

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()

passman.add_password(None, urll, username, password)

authhandler = urllib2.HTTPBasicAuthHandler(passman)

urllib2 = urllib2.build_opener(authhandler)

pagehandle = urllib.urlopen(urll)

print (pagehandle.read())

This all works, but it's prompting the username and password via the command line, requiring the user's interaction. I want it to automatically enter those values. What am I doing wrong?

这一切都有效,但它通过命令行提示用户名和密码,需要用户交互。我希望它自动输入这些值。我究竟做错了什么?

采纳答案by mortezaipo

Your request url is "RESTRICTED".

您的请求网址是“受限”。

If you try this code it will tell you:

如果您尝试此代码,它会告诉您:

import urllib2
theurl = 'https://updates.opendns.com/nic/update?hostname='
req = urllib2.Request(theurl)
try:
    handle = urllib2.urlopen(req)
except IOError, e:
    if hasattr(e, 'code'):
        if e.code != 401:
            print 'We got another error'
            print e.code
        else:
            print e.headers
            print e.headers['www-authenticate']

You should add authorization headers. For more details have a look into: http://www.voidspace.org.uk/python/articles/authentication.shtml

您应该添加授权标头。有关更多详细信息,请查看:http: //www.voidspace.org.uk/python/articles/authentication.shtml

And another code example is: http://code.activestate.com/recipes/305288-http-basic-authentication/

另一个代码示例是:http: //code.activestate.com/recipes/305288-http-basic-authentication/

If you wish to send POST request , try it:

如果您想发送 POST 请求,请尝试:

import urllib
import urllib2
username = "username"
password = "password"
url = 'http://url.com/'
values = { 'username': username,'password': password }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
print result

Note:this is just an example of how to send POST request to URL.

注意:这只是如何向 URL 发送 POST 请求的示例。

回答by James Mason

I haven't played with python in awhile, but try this:

我有一段时间没有玩过 python,但试试这个:

urllib.urlopen("http://username:[email protected]/path")

回答by Ion Scerbatiuc

You could use requestsinstead. The code is as simple as:

您可以改用请求。代码很简单:

import requests
url = 'https://updates.opendns.com/nic/update?hostname='
username = 'username'
password = 'password'
print(requests.get(url, auth=(username, password)).content)