- redis
- mysql
- Fabric
- 安装失败
## json
使用dump()
方法将对象序列化成json,然后使用load()
将字符串反序列化成对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import json
list = [123, "ad"] listJson = json.dumps(list) listR = json.loads(listJson) print "列表序列化 : " + listJson print "列表反序列化 : " + str(listR[0])
tumple = (123, "adf") tumpleJson = json.dumps(tumple) tumpleR = json.loads(tumpleJson) print "元组序列化 : " + tumpleJson print "元组反序列化 : " + str(tumpleR[1])
map = { "key1":"value1", "key2":"value2", "key3":"value3", } mapJson = json.dumps(map) mapR = json.loads(mapJson) print "字典序列化 : " + mapJson print "字典反序列化 : " + str(mapR["key1"])
seq = ['apple', 'mango', 'carrot', 'banana'] seqJson = json.dumps(seq) seqR = json.loads(seqJson) print "序列序列化 : " + seqJson print "序列反序列化 : " + str(seqR[1])
type tumpleR[1] type mapR["key1"] type seqR[1]
|
redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
import sys reload(sys) sys.setdefaultencoding('utf-8')
import redis import json
_REDIS_HOST = 'localhost' _REDIS_PORT = 6379 _REDIS_DB = 1 _PASSWORD = "2016"
def getRedisCli() : redisCli = redis.Redis(host=_REDIS_HOST, port=_REDIS_PORT, db=_REDIS_DB, password=_PASSWORD) return redisCli
def info() : redisCli = getRedisCli() return redisCli.info()
def slowlog_get() : redisCli = getRedisCli() return redisCli.slowlog_get()
def client_list() : redisCli = getRedisCli() return redisCli.client_list()
def dbsize() : redisCli = getRedisCli() return redisCli.dbsize()
info = info() slowlog_get = slowlog_get() dbsize = dbsize() print info
|
mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import sys import mysql.connector
mysql_host = "localhost" mysql_database = "test" mysql_user = "root" mysql_passwrold = "root"
reload(sys) sys.setdefaultencoding('utf-8')
def exec_sqls(exec_sql): cnx = mysql.connector.connect(user=mysql_user, password=mysql_passwrold, host=mysql_host, database=mysql_database) cursor = cnx.cursor() exec_sql(cursor) cnx.commit() cursor.close() cnx.close()
def query_max_connections(cursor): print "连接数信息"
max_connections = exec_sql_query(cursor, "show variables like 'max_connections'") max_connections_num = float(max_connections[0][1]) print("max_connections : " + str(max_connections_num)) exec_sqls(query_max_connections)
|
Fabric
安装
如果已经安装过了则进行更新, 我现在基于的是1.12.0版本
1
| pip install --upgrade fabric
|
执行本地命令
1 2 3 4 5 6 7 8 9 10 11 12 13
|
import sys reload(sys) sys.setdefaultencoding('utf-8')
from fabric.api import local
def echo_helloworld(): local("ECHO helloworld")
echo_helloworld()
|
如果命令报错的话,我们进行错误处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import sys reload(sys) sys.setdefaultencoding('utf-8')
from fabric.api import local, settings
def cd_test_dir(): with settings(warn_only=True): result = local("cd ./test", capture=True) if result.failed: local("mkdir ./test") local("cd ./test", capture=True)
cd_test_dir()
|
安装失败
最近在mac上使用pip安装插件,总是提示
1 2 3 4 5 6 7
| Exception: Traceback (most recent call last): File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in ... ... ... OSError: [Errno 1] Operation not permitted: '/tmp/pip-qo8UFu-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
|
各种su
, sudo
都不行, 在百度上找到一种解决方案
1
| pip install scrapy --user -U
|
基于用户的权限来安装模块包, 成功安装