Python学习

一、安装Python3

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

 测试用例:

 五、下载文件

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

 

七、格式化输出磁盘容量

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()

 

posted @ 2016-04-12 15:30  糖豆爸爸  阅读(475)  评论(0编辑  收藏  举报
Live2D