1.shelve模块
写:1
2
3
4
5
6
7
8
9
10
11#!/usr/bin/env python
import shelve
zhangsan = dict(zip(['name','age'],['zhangsan',24]))
lisi = dict(zip(['name','age'],['lisi',25]))
db = shelve.open('shelveDict')
db['zhangsan'] = zhangsan
db['lisi'] = lisi
db.close()
读:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#!/usr/bin/env python
import shelve
db = shelve.open('shelveDict')
print db['zhangsan']
print db['lisi']
db.close()
修改:
#!/usr/bin/env python
import shelve
db = shelve.open('shelveDict')
zhangsan = db['zhangsan']
zhangsan['name'] = 'zhangsanfeng'
db['zhangsan'] = zhangsan
print db['zhangsan']
db.close()
2.httplib模块
GET请求:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import httplib
class HttpRequestGETTest(object):
def __init__(self):
#self.body='{"UserName":"Admin","Password":"693aa8d0806c532115637809a863b1a3","sessionID":""}'
self.headers = {
"Referer": '192.168.1.1',
"Accept-Encoding": "gzip, deflate,sdch",
"Connection":"Keep-Alive"}
def http_get(self):
conn=httplib.HTTPConnection(host='192.168.1.1', port=80, strict=False, timeout=30)
conn.request(method='GET',url='/cgi-bin/GetLoginStatus?sessionID=undefined', body=None, headers=self.headers)
a = conn.getresponse().read()
print a
lianxi=HttpRequestGETTest()
lianxi.http_get()
POST请求:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import httplib
class HttpRequestPOSTTest(object):
def __init__(self):
self.body='{"UserName":"Admin","Password":"693aa8d0806c532115637809a863b1a3","sessionID":""}'
self.headers = {
"Referer": '192.168.1.1',
"Accept-Encoding": "gzip, deflate,sdch",
"Connection":"Keep-Alive"}
def http_post(self):
conn=httplib.HTTPConnection(host='192.168.1.1', port=80, strict=False, timeout=120)
conn.request(method='POST',url='/cgi-bin/Login', body=self.body, headers=self.headers)
self.session_id = conn.getresponse().read()
print self.session_id
lianxi=HttpRequestPOSTTest()
lianxi.http_post()
3.ftplib模块
1 | #coding: utf-8 |
4.urllib模块
1 | >>> import urllib |
5.hashlib模块
1 | #!/usr/bin/env python |
6.tarfile模块
使用tarfile压缩
1
2
3
4
5
6
7
8
9
10
11
12
13#!/usr/bin/env python
# coding:utf-8
import tarfile
#创建压缩包名
tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")
#创建压缩包
for root,dir,files in os.walk("/tmp/tartest"):
for file in files:
fullpath = os.path.join(root,file)
tar.add(fullpath)
tar.close()使用tarfile解压
1
2
3
4
5
6
7#!/usr/bin/env python
# coding:utf-8
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()