参考:

  • 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)的更多相关文章

  1. HTTP基本认证(Basic Authentication)的JAVA实例代码

    大家在登录网站的时候,大部分时候是通过一个表单提交登录信息. 但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证. 下面来看看一看这个认证的工作过程: 第一步: 客户端发送 ...

  2. HTTP基础认证Basic Authentication

    HTTP基础认证Basic Authentication Basic Authentication是一种HTTP访问控制方式,用于限制对网站资源的访问.这种方式不需要Cookie和Session,只需 ...

  3. Nancy 学习-身份认证(Basic Authentication) 继续跨平台

    开源 示例代码:https://github.com/linezero/NancyDemo 前面讲解Nancy的进阶部分,现在来学习Nancy 的身份认证. 本篇主要讲解Basic Authentic ...

  4. HTTP基本认证(Basic Authentication)的JAVA示例

    大家在登录网站的时候,大部分时候是通过一个表单提交登录信息.但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证.下面来看看一看这个认证的工作过程:第一步:  客户端发送ht ...

  5. HTTP基本认证(Basic Authentication)的JAVA演示样例

    大家在登录站点的时候.大部分时候是通过一个表单提交登录信息.可是有时候浏览器会弹出一个登录验证的对话框.例如以下图,这就是使用HTTP基本认证.以下来看看一看这个认证的工作过程:第一步:  clien ...

  6. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  7. HTTP Basic Authentication认证的各种语言 后台用的

    访问需要HTTP Basic Authentication认证的资源的各种语言的实现 无聊想调用下嘀咕的api的时候,发现需要HTTP Basic Authentication,就看了下. 什么是HT ...

  8. 访问需要HTTP Basic Authentication认证的资源的各种开发语言的实现

    什么是HTTP Basic Authentication?直接看http://en.wikipedia.org/wiki/Basic_authentication_scheme吧. 在你访问一个需要H ...

  9. HTTP Basic Authentication认证(Web API)

    当下最流行的Web Api 接口认证方式 HTTP Basic Authentication: http://smalltalllong.iteye.com/blog/912046 什么是HTTP B ...

随机推荐

  1. 【kAri OJ 616】Asce的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 作为一个东北大老爷们,大A熊以力气大著称,现在有一颗半径为r的树,剖面图如黑色的圆,大A熊决定搬几个半径为R的圆柱形桶将其围住,剖面图如 ...

  2. 【CodeForces 602A】C - 特别水的题3-Two Bases

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/C Description After seeing the ...

  3. Oracle中的伪列

    分页查询中,需要用到伪列rownum,代码如下: select * from (select rownum rn, name from cost where rownum <= 6) where ...

  4. codevs1322 单词矩阵

    题目描述 Description 对于包含字母A到Y各一次的单词S,将其从上到下从左到右写在一个5*5的矩阵中,如单词ADJPTBEKQUCGLRVFINSWHMOXY写出来如下: A D J P T ...

  5. 超级懒汉编写的基于.NET的微信SDK

    一.前言 特别不喜欢麻烦的一个人,最近碰到了微信开发.下载下来了一些其他人写的微信开发“框架”,但是被恶心到了,实现的太臃肿啦. 最不喜欢的就是把微信返回的xml消息在组装成实体类,所以会比较臃肿,现 ...

  6. vim YouCompleteMe

    http://www.ithao123.cn/content-1906969.html http://www.it165.net/os/html/201503/12190.html

  7. 02 C语言指针

    今天发帖记录自己学习C语言精髓的心理历程,人生就像是一次旅途,沿途总是能看到最美的风景,让我们的思想相逢在C语言中. 一 初识指针,指针的定义 指针是C语言中的一种类型,类似于整形,字符型等.既然C指 ...

  8. ExtJS入门教程02,form也可以很优雅

    在上一篇<Extjs window 入门>中,我们已经看到了如何将一个form组件放到window中,今天我们来看看form的一些优雅的工作方式. 使用fieldDefaults,优雅的设 ...

  9. gdb命令与调试方法

    单线程 http://www.cnblogs.com/lidabo/p/5629830.html 编译程序一定要加-g选项 gcc -g test.c -o test 进入gdb调试:gdb 程序名 ...

  10. IIS6.0文件解析漏洞小结

    今天搞站,本来这个站是aspx的,webserver是IIS6.0的,进入后台之后,发现有一个上传图片的地方,于是,我就上传了一张asp/aspx的一句话图片木马,但是用菜刀连接的时候,没有成功get ...