一、安装Python3
1、CentOS6.5 安装Python 的依赖包yum groupinstall "Development tools" -yyum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -ycd /usr/local/software && wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgztar xf Python-3.5.0.tgzcd Python-3.5.0./configure -prefix=/usr/local/python -enable-sharedmake && make installln -s /usr/local/python/bin/python3 /usr/bin/python33、在运行Python之前需要配置库:echo /usr/local/python/lib >> /etc/ld.so.conf.d/local.conf && ldconfig4、运行演示:python3 -VPython 3.5.0 5、升级一下pip pip3 install --upgrade pip
二、安装Mysql
pip3 install PyMySQL
测试用例:
import pymysql.cursors# Connect to the databaseconnection = pymysql.connect(host='10.10.6.199', port=22066, user='root', password='****************', db='dsideal_db', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)try: with connection.cursor() as cursor: # Create a new record sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" cursor.execute(sql, ('webmaster@python.org', 'very-secret')) # connection is not autocommit by default. So you must commit to save # your changes. connection.commit() with connection.cursor() as cursor: # Read a single record sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s" cursor.execute(sql, ('webmaster@python.org',)) result = cursor.fetchone() print(result)finally: connection.close()
三、安装redis
pip3 install redispip3 install hiredis
测试用例:
import redis pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0) r = redis.StrictRedis(connection_pool = pool) r.set('foo', 'bar') q=r.get('foo') print(q.decode('utf-8'))
四、安装ssdb
pip3 install ssdb.py
测试用例:
五、下载文件
pip3 install requests
import requestsprint("downloading with requests")url = 'http://www.****.com/test/demo.zip'r = requests.get(url)with open("demo3.zip", "wb") as code: code.write(r.content)
换一种方式进行:
import urllib, urllib.request, urllib.parse, urllib.errorprint("downloading with urllib")url = 'https://www.baidu.com/img/bd_logo1.png'f = urllib.request.urlopen(url)data = f.read()with open("bd_logo1.png", "wb") as code: code.write(data)
六、监控服务器情况
pip3 install psutil --upgrade
在python3中的psutil的使用办法
七、格式化输出磁盘容量
def getSizeInNiceString(sizeInBytes): """ Convert the given byteCount into a string like: 9.9bytes/KB/MB/GB """ for (cutoff, label) in [(1024*1024*1024, "GB"), (1024*1024, "MB"), (1024, "KB"), ]: if sizeInBytes >= cutoff: return "%.1f %s" % (sizeInBytes * 1.0 / cutoff, label) if sizeInBytes == 1: return "1 byte" else: bytes = "%.1f" % (sizeInBytes or 0,) return (bytes[:-2] if bytes.endswith('.0') else bytes) + ' bytes'测试用例:print(getSizeInNiceString(1024*1024*9234234234))
八、SMTP发送邮件
from email import encodersfrom email.header import Headerfrom email.mime.text import MIMETextfrom email.utils import parseaddr, formataddrimport smtplibdef _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), addr))from_addr = "10402852@qq.com"password = "************"to_addr = "10402852@qq.com"smtp_server = "smtp.qq.com"msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr)msg['To'] = _format_addr('管理员 <%s>' % to_addr)msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()server = smtplib.SMTP(smtp_server, 25)server.set_debuglevel(1)server.login(from_addr, password)server.sendmail(from_addr, [to_addr], msg.as_string())server.quit()