您的位置:首页 > 编程语言 > Go语言

goagent用到的模块简介

2012-05-31 10:28 127 查看
1. base64

base64模块是用来作base64编码解码的。这种编码方式在电子邮件中是很常见的。

它可以把不能作为文本显示的二进制数据编码为可显示的文本信息。编码后的文本大小会增大1/3。

闲话不说了,base64模块真正用的上的方法只有8个,分别是encode, decode, encodestring, decodestring, b64encode,b64decode, urlsafe_b64decode,urlsafe_b64encode。他们8个可以两两分为4组,encode,decode一组,专门用来编码和 解码文件的,也可以对StringIO里的数据做编解码;encodestring,decodestring一组,专门用来编码和解码字符串; b64encode和b64decode一组,用来编码和解码字符串,并且有一个替换符号字符的功能。这个功能是这样的:因为base64编码后的字符除
了英文字母和数字外还有三个字符 + / =, 其中=只是为了补全编码后的字符数为4的整数,而+和/在一些情况下需要被替换的,b64encode和b64decode正是提供了这样的功能。至于什 么情况下+和/需要被替换,最常见的就是对url进行base64编码的时候。urlsafe_b64encode和urlsafe_b64decode 一组,这个就是用来专门对url进行base64编解码的,实际上也是调用的前一组函数。

def proxy_basic_auth_header(self):
return 'Proxy-Authorization: Basic %s' + base64.b64encode('%s:%s'%(self.PROXY_USERNAME, self.PROXY_PASSWROD))


2. urllib2

在网上找了一下关于urllib2模块的介绍,都比较简单,下面是从python文档中找的,贴了一部分。

一句话概括的话:urllib2是一个可扩展的库,可根据不同的协议打开URLs。

>>> help (urllib2)
Help on module urllib2:

NAME
urllib2 - An extensible library for opening URLs using a variety of protocols

FILE
c:\python27\lib\urllib2.py

DESCRIPTION
The simplest way to use this module is to call the urlopen function,
which accepts a string containing a URL or a Request object (described
below).  It opens the URL and returns the results as file-like
object; the returned object has some extra methods described below.

The OpenerDirector manages a collection of Handler objects that do
all the actual work.  Each Handler implements a particular protocol or
option.  The OpenerDirector is a composite object that invokes the
Handlers needed to open the requested URL.  For example, the
HTTPHandler performs HTTP GET and POST requests and deals with
non-error returns.  The HTTPRedirectHandler automatically deals with
HTTP 301, 302, 303 and 307 redirect errors, and the HTTPDigestAuthHandler
deals with digest authentication.

urlopen(url, data=None) -- Basic usage is the same as original
urllib.  pass the url and optionally data to post to an HTTP URL, and
get a file-like object back.  One difference is that you can also pass
a Request instance instead of URL.  Raises a URLError (subclass of
IOError); for HTTP errors, raises an HTTPError, which can also be
treated as a valid response.

build_opener -- Function that creates a new OpenerDirector instance.
Will install the default handlers.  Accepts one or more Handlers as
arguments, either instances or Handler classes that it will
instantiate.  If one of the argument is a subclass of the default
handler, the argument will be installed instead of the default.

install_opener -- Installs a new opener as the default opener.


def install_opener(self):
if self.PROXY_ENABLE:
proxy = '%s:%s@%s:%d'%(self.PROXY_USERNAME, self.PROXY_PASSWROD, self.PROXY_HOST, self.PROXY_PORT)
handlers = [urllib2.ProxyHandler({'http':proxy,'https':proxy})]
if self.PROXY_NTLM:
if ntlm is None:
logging.critical('You need install python-ntlm to support windows domain proxy! "%s:%s"', self.PROXY_HOST, self.PROXY_PORT)
sys.exit(-1)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, '%s:%s' % (self.PROXY_HOST, self.PROXY_PORT), self.PROXY_USERNAME, self.PROXY_PASSWROD)
auth_NTLM = ntlm.HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
handlers.append(auth_NTLM)
else:
handlers = [urllib2.ProxyHandler({})]
opener = urllib2.build_opener(*handlers)
opener.addheaders = []
urllib2.install_opener(opener)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: