标签:
# -*- coding: utf-8 -*-
# python:2.x
__author__ = ‘Administrator‘
import urllib2
#例子
LOGIN=‘wesc‘
PASSWD="you‘llNeverGuess"
URL=‘http://localhost‘
def h1(url):
from urlparse import urlparse as up
hdlr=urllib2.HTTPBasicAuthHandler()
hdlr.add_password(‘Archives‘,up(url)[1],LOGIN,PASSWD)
opener=urllib2.build_opener(hdlr)
urllib2.install_opener(opener)
return url
def req(url):
from base64 import encodestring as s
req1=urllib2.Request(url)
b64str=s(‘%s:%s‘%(LOGIN,PASSWD))[:-1]
req1.add_header(‘Authorization‘,‘Basic %s‘%b64str)
return req1
for s in (‘handler‘,‘request‘):
print ‘***using %s:‘%s.upper()
url=eval(‘req‘)(URL)
f=urllib2.urlopen(url)
print f.readline()
f.close()
标签:
原文地址:http://www.cnblogs.com/mhxy13867806343/p/3968751.html