Python学习
2016-04-12 15:30
501 查看
一、安装Python3
二、安装Mysql
测试用例:
三、安装redis
测试用例:
四、安装ssdb
测试用例:
五、下载文件
换一种方式进行:
六、监控服务器情况
七、格式化输出磁盘容量
八、SMTP发送邮件
1、CentOS6.5 安装Python 的依赖包 yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y cd /usr/local/software && wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz tar xf Python-3.5.0.tgz cd Python-3.5.0 ./configure -prefix=/usr/local/python -enable-shared make && make install ln -s /usr/local/python/bin/python3 /usr/bin/python3 3、在运行Python之前需要配置库: echo /usr/local/python/lib >> /etc/ld.so.conf.d/local.conf && ldconfig 4、运行演示: python3 -V Python 3.5.0 5、升级一下pip pip3 install --upgrade pip
二、安装Mysql
pip3 install PyMySQL
测试用例:
import pymysql.cursors # Connect to the database connection = 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 redis pip3 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
测试用例:
https://github.com/hit9/ssdb.py/blob/master/tests.py
五、下载文件
pip3 install requests
import requests print("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.error print("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的使用办法 http://www.jianshu.com/p/8e88ade71986 https://pypi.python.org/pypi/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 encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr import smtplib def _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()
相关文章推荐
- python高速排序
- 保留最后N个元素
- Python modify string in place
- 扩展python
- python中的requests用法简介
- python面对对象编程---------6:抽象基类
- python win32api.ExitWindows() 恶作剧好东西
- Python小程序
- Awesome Python Books Awesome
- Python - access network
- 初学python(对比java语言不同) 第七篇
- [Python]输出中文报错的解决方法
- sublime text3安装SublimeREPL,并配置python交互式终端快捷键
- Python列表操作——模拟实现栈和队列
- python脚本实例002- 利用requests库实现应用登录
- python 爬虫The One的 数据两种方式
- python面对对象编程-------5:获取属性的四种办法:@property, __setattr__(__getattr__) ,descriptor
- python部落刷题宝学到的内置函数(二)
- python 实时遍历日志文件
- 完全用Python工作---Harness the power of Python