参考:

  • 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. jQuery1.4源码解读

    来吧, 慢慢折腾吧 总结一下: jq1.4挺简单的, 正则写的不多, 看的都懂, 多写一些 三目写法到底要不要 特殊的地方的注释一定要有 /*! * jQuery JavaScript Library ...

  2. Java算法-希尔排序

    希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题.希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, ...

  3. Throwable和Exception的区别

    Java语言要求java程序中(无论是谁写的代码)所有抛出(throw)的异常都必须是从Throwable派生而来.当然,实际的Java编程中,由于JDK平台已经为我们设计好了非常丰富和完整的异常对象 ...

  4. c#截图

    private void Form_Load(object sender, EventArgs e){  //接收web url  string colle = string.Empty;  stri ...

  5. 洛谷P1202 [USACO1.1]黑色星期五Friday the Thirteenth

    题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...

  6. 洛谷P1121 环状最大两段子段和

    题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件maxsum2.in的第一行是一个正整数N,表示了序列 ...

  7. wifi与wimax

    这几天在看文献中看到802.11a,802.11n和802.16e这几种无信通信协议标准,在网上查了相关资料后,看到有个帖子总结得不错,故将其转载过来. 转:http://blog.csdn.net/ ...

  8. JSTL标签库简介

    核心标签库 http://java.sun.com/jsp/jstl/core <c:catch>,<c:url>的使用 <!-- 捕获异常 --> <c:c ...

  9. 统计网站访问量,以GD2库图像形式输出

    index.php页面<?php session_start(); if($_SESSION[temp]==""){ //判断$_SESSION[temp]=="& ...

  10. WCF中的标准绑定

    使用过WCF的童鞋们都很清楚,绑定是必须的.我将这些绑定总结了下. 一.标准绑定简要说明 1.basicHttpBinding 基于WS-I Basic Profile 1.1 的web服务,所需的. ...