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编解码的,实际上也是调用的前一组函数。
2. urllib2
在网上找了一下关于urllib2模块的介绍,都比较简单,下面是从python文档中找的,贴了一部分。
一句话概括的话:urllib2是一个可扩展的库,可根据不同的协议打开URLs。
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)
相关文章推荐
- goagent新功能个人配置文件proxy.user.ini使用简介
- 【Away3D代码解读】(四):主要模块简介
- 微软企业库4.1学习笔记(二十四)Unity依赖注入模块 简介
- Python的jieba模块简介
- 【Away3D代码解读】(四):主要模块简介
- LWP::Simple 模块功能简介
- SAP SD模块-公司间销售简介
- 安信可A7模块、DB9公母头及TTL\rs232简介
- 树莓小车-超声波模块HC-SR04简介以及编程
- PP模块快速入门之功能简介(三)
- 零基础自学用Python 3开发网络爬虫(二): 用到的数据结构简介以及爬虫Ver1.0 alpha
- Linux内核分析(二)----内核模块简介|简单内核模块实现
- Qt 模块简介
- Python logging 模块简介
- zen cart PayPal支付模块简介
- OpenCV模块以及功能简介
- 变长参数的函数(在软件的日志模块经常要用到)
- Qt 模块简介
- Adhesive框架系列文章--配置服务模块简介