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 ...
随机推荐
- 负margin一些奇葩的布局技巧
copy_from_ http://www.hicss.net/i-know-you-do-not-know-the-negative-margin/ <!doctype html> &l ...
- BZOJ1036 树的统计
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- 思维导图XMiand
XMiand: 异常强大的国产思维导图工具,还能将图同步到服务器上.做思维导图和头脑风暴必备软件,还能转换绘制鱼骨图.二维图.树形图.逻辑图.组织结构图.
- maven运行javaWeb项目
首先从svn下载下来的maven项目,需要点击项目,然后import--->Existing Maven Projects->全选之后点next就转换成功了,然后 run as--> ...
- JS 在线网站
JS 在线压缩网站 国内压缩js网站http://tool.css-js.com/ uglifyjs 压缩js网站http://lisperator.net/uglifyjs JS在线格式化网站 ht ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- Struts2 自定义Result
注意:我只要是解决自定义返回Json 和异常处理问题 新建一个类 AjaxResult 继承 StrutsResultSupport 看看代码吧 public class AjaxResult e ...
- 过滤器在Web开发应用------解决中文乱码
src/com/Encoding.java package com; import java.io.IOException; import javax.servlet.Filter; import j ...
- sprintf
功能:将数据格式化到字符串中 原型:int sprintf( char *buffer, const char *format, [ argument] … );返回值是这个字符串的长度 上次我企图这 ...
- Matplotlib中文设置
1.中文设置方法,代码前加入语句 from pylab import mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] 2.例子 # -*- codin ...