与urllib3认证 [英] authentication with urllib3

查看:445
本文介绍了与urllib3认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到使用urllib3的网页。在code如下。

I am trying to connect to a webpage using urllib3. The code is provided below.

import urllib3
http=urllib3.PoolManager()
fields={'username':'abc','password':'xyz')
r=http.request('GET',url,fields)

如果我们假设URL是一些网页,需要使用用户名和密码进行身份验证,现在用的是正确的code来验证我?

If we assume that url is some webpage which needs to be authenticated using username and password, am i using the right code to authenticate ?

我已经这样做使用的urllib2很舒服,但我没能使用urllib3做同样的事情。

I have did this using urllib2 very comfortably but i was not able to do the same thing using urllib3.

非常感谢

推荐答案

假设你正在试图做的基本验证,那么你就需要把EN codeD上的用户名和密码在授权头。下面是做到这一点使用 urllib3.util.make_headers 助手:

Assuming you're trying to do Basic Authentication, then you need to put the username and password encoded in an authorization header. Here's one way to do that using the urllib3.util.make_headers helper:

import urllib3

http = urllib3.PoolManager()
url = '...'
headers = urllib3.util.make_headers(basic_auth='abc:xyz')
r = http.request('GET', url, headers=headers)

这篇关于与urllib3认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆