PYTHON实现HTTP基本认证(BASIC AUTHENTICATION)
参考:
- http://www.voidspace.org.uk/python/articles/authentication.shtml#id20
- http://zh.wikipedia.org/wiki/HTTP%E5%9F%BA%E6%9C%AC%E8%AE%A4%E8%AF%81
#! /usr/bin/env python
# -*-coding:utf-8-*- import re
import sys
import base64
import urllib2 class BasicAuth:
def __init__(self, username, password, realm=''):
base_str = "%s:%s" % (username,password)
base_str = "Basic " + base64.encodestring(base_str)[:-1]
self.authline = base_str
self.realm = realm
#print self.authline def visit(self, the_url):
req = urllib2.Request(the_url)
try:
content = urllib2.urlopen(req)
except IOError,e:
#here we *want* fail
pass
else:
print "This page isn't protected by authentication."
sys.exit(1) if not hasattr(e, 'code') or e.code != 401:
#we got an error - but not a 401 error
print "This page isn't protected by authentication."
print 'But we fail for another reason'
sys.exit(1) authline = e.headers['www-authenticate']
print authline
#print e.headers
authobj = re.compile( r'''(?:\s*www-authenticate\s*:)?\s*(\w*)\s+realm=['"]([^'"]+)['"]''',re.IGNORECASE)
matchobj = authobj.match(authline)
if not matchobj:
print 'The authentication header is badly formed.'
print authline
sys.exit(1)
scheme = matchobj.group(1)
realm = matchobj.group(2)
if scheme.lower() != 'basic':
print 'This example only work with BASIC authentication.'
sys.exit(1) req.add_header("Authorization", self.authline)
try:
handle = urllib2.urlopen(req)
except IOError,e:
print "It looks like the username or password is wrong."
sys.exit(1)
thepage = handle.read()
return thepage if __name__ == "__main__":
ba = BasicAuth('admin', 'admin')
content = ba.visit("http://192.168.1.1/images/logo.jpg") #路由器管理页面通常采用基本认证法进行身份认证
with open('logo.jpg', 'w') as f:
f.write(content)
Python升级版:
#! /usr/bin/env python
# -*-coding:utf-8 -*- import urllib2 theurl = 'http://192.168.1.1'
username = 'admin'
password = 'admin' passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener) pagehandle = urllib2.urlopen('http://192.168.1.1/images/logo.jpg')
#with open('tplogo.jpg', 'w') as f:
#f.write(pagehandle.read())
Python终极版:
import requests #http://www.itwhy.org/%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B/python/python-%E7%AC%AC%E4%B8%89%E6%96%B9-http-%E5%BA%93-requests-%E5%AD%A6%E4%B9%A0.html
r = requests.get('http://192.168.0.1/',auth=('admin','admin'))
Bash终极版
curl -u admin:admin http://192.168.1.1/images/logo.jpg -v
PYTHON实现HTTP基本认证(BASIC AUTHENTICATION)的更多相关文章
- HTTP基本认证(Basic Authentication)的JAVA实例代码
大家在登录网站的时候,大部分时候是通过一个表单提交登录信息. 但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证. 下面来看看一看这个认证的工作过程: 第一步: 客户端发送 ...
- HTTP基础认证Basic Authentication
HTTP基础认证Basic Authentication Basic Authentication是一种HTTP访问控制方式,用于限制对网站资源的访问.这种方式不需要Cookie和Session,只需 ...
- Nancy 学习-身份认证(Basic Authentication) 继续跨平台
开源 示例代码:https://github.com/linezero/NancyDemo 前面讲解Nancy的进阶部分,现在来学习Nancy 的身份认证. 本篇主要讲解Basic Authentic ...
- HTTP基本认证(Basic Authentication)的JAVA示例
大家在登录网站的时候,大部分时候是通过一个表单提交登录信息.但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证.下面来看看一看这个认证的工作过程:第一步: 客户端发送ht ...
- HTTP基本认证(Basic Authentication)的JAVA演示样例
大家在登录站点的时候.大部分时候是通过一个表单提交登录信息.可是有时候浏览器会弹出一个登录验证的对话框.例如以下图,这就是使用HTTP基本认证.以下来看看一看这个认证的工作过程:第一步: clien ...
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
- HTTP Basic Authentication认证的各种语言 后台用的
访问需要HTTP Basic Authentication认证的资源的各种语言的实现 无聊想调用下嘀咕的api的时候,发现需要HTTP Basic Authentication,就看了下. 什么是HT ...
- 访问需要HTTP Basic Authentication认证的资源的各种开发语言的实现
什么是HTTP Basic Authentication?直接看http://en.wikipedia.org/wiki/Basic_authentication_scheme吧. 在你访问一个需要H ...
- HTTP Basic Authentication认证(Web API)
当下最流行的Web Api 接口认证方式 HTTP Basic Authentication: http://smalltalllong.iteye.com/blog/912046 什么是HTTP B ...
随机推荐
- qt添加最小化和关闭按钮
int width = this->width();//获取界面的宽度 //构建最小化.最大化.关闭按钮 QToolButton *minButton = new QToolButton(thi ...
- Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before)
一.不得不提的volatile volatile是个很老的关键字,几乎伴随着JDK的诞生而诞生,我们都知道这个关键字,但又不太清楚什么时候会使用它:我们在JDK及开源框架中随处可见这个关键字,但并发专 ...
- Windows python3.3下安装BeautifulSoup
首先在官网下载:http://www.crummy.com/software/BeautifulSoup/#Download BeautifulSoup在版本4以上都开始支持python3了,所以就下 ...
- BZOJ-3231 递归数列 矩阵连乘+快速幂
题不是很难,但是啊,人很傻啊...机子也很鬼畜啊... 3231: [Sdoi2008]递归数列 Time Limit: 1 Sec Memory Limit: 256 MB Submit: 569 ...
- Linux 基础网络设置
一.查看以及测试网络 查看及测试网络配置是管理Linux网络服务的第一步,本节将学习Linux系统中的网络查看以及测试命令.其中讲解的大多数命令以普通用户权限就可以完成操作,但是普通用户在执行&quo ...
- Redis 集合操作
1.SCARD key 返回集合 key 的基数(集合中元素的数量). 2.SDIFFSTORE destination key [key ...] 这个命令的作用和 类似,但它将结果保存到 des ...
- Spring学习7-Spring整合Hibernate
一.Springl为什么要整合Hibernate 二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...
- java内存模型及GC原理
java内存模型 sun官方网站:sun java 虚拟机模型 JVM内存模型中分两大块,一块是 NEW Generation, 另一块是Old Generation. 在New Generation ...
- HTTP负载测试——Tsung
参考资料:http://blog.jobbole.com/87509/ 如何生成每秒百万级别的 HTTP 请求? 在进行负责测试时要牢记一件重要的事:你能在 Linux 上建立多少个 socket 连 ...
- 笔记:PHP查询mysql数据后中文字符乱码
新建表Clubs CREATE TABLE `Clubs` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) CHARACTER SET utf8 NOT NULL ...