博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python学习
阅读量:6848 次
发布时间:2019-06-26

本文共 3840 字,大约阅读时间需要 12 分钟。

一、安装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()

 

转载地址:http://bdrul.baihongyu.com/

你可能感兴趣的文章
MySQL 批量写入数据报错:mysql_query:Lost connection to MySQL server during query
查看>>
【spring boot】spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面...
查看>>
简约响应式布局样式
查看>>
iOS平台XML解析类库对比和安装说明
查看>>
各种纪念-好久没更新了
查看>>
渴望出差
查看>>
非常酷的国外网站导航设计案例欣赏
查看>>
xp sp3+iis 服务器不可用
查看>>
【windows phone】控件1
查看>>
使用curl抓取网页遇到HTTP跳转时得到多个HTTP头部的问题
查看>>
JQuery学习笔记
查看>>
UITextField的详细使用
查看>>
oracle触发器select into和cursor用法的区别
查看>>
TortoiseGit + msysgit 记住帐号密码方法及使用密匙的方法
查看>>
Python中的else
查看>>
zend_db连接mysql(附完整代码)(转)
查看>>
五个人二个月为什么不等于十个人一个月
查看>>
matlab 与 VC 混编函数参数传递<2>
查看>>
Silverlight for Windows Phone开发系列课程
查看>>
Ajax经典交互讲解
查看>>