python中url是什么_连接到python中的url

我正在尝试使用以下代码连接到具有用户名和密码的URL:

urllib.request.urlopen("http://username:password@......etc...", None)

但我得到了

urllib.error.URLError: urlopen error [Errno 11003] getaddrinfo failed

有谁知道怎么了?

解决方法:

对不起.我没注意到你在使用py3k.

见urllib.request – FancyURLopener.我个人不太了解py3k.

基本上,您需要子类化urllib.request.FancyURLopener,覆盖prompt_user_passwd(host,realm),然后调用YourClass.urlopen(url).

下面是py2

这就是你想要的,urllib2 – Basic Authentication

下面是该页面的代码,以防万一有些日子链接腐烂.

# create a password manager

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.

# If we knew the realm, we could use it instead of None.

top_level_url = "http://example.com/foo/"

password_mgr.add_password(None, top_level_url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)

opener = urllib2.build_opener(handler)

# use the opener to fetch a URL

opener.open(a_url)

# Install the opener.

# Now all calls to urllib2.urlopen use our opener.

urllib2.install_opener(opener)

标签:python,http,authentication,python-3-x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值