这里使用python3版本 使用pyenv进行python多版本管理 这里使用使用的系统CentOS release 6.5 (Final) 1.安装pyenv [root@localhost ~]# curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash 编辑~/.bash_profile # the following to ~/.bash_profile: export PATH="/root/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" 2.安装python3.4.2 [root@localhost ~]# pyenv install 3.4.2 [root@localhost ~]# pyenv local 3.4.2 3.pyenv使用 pyenv versions 查看当前 pyenv 可检测到的所有版本,处于激活状态的版本前以 * 标示。 $ pyenv versions system 3.4.2 3.4.2/envs/eddy_001 3.5.0 * eddy_001 (set by /root/magedu/.python-version) pyenv version 查看当前处于激活状态的版本,括号中内容表示这个版本是由哪条途径激活的(global、local) $ pyenv version eddy_001 (set by /root/magedu/.python-version pyenv uninstall 卸载一个版本 $ pyenv uninstall eddy_001 为所有已安装的可执行文件 (如:~/.pyenv/versions/*/bin/*) 创建 shims,因此,每当你增删了 Python 版本或带有可执行文件的包(如 pip)以后,都应该执行一次本命令 $ pyenv install 3.4.2 $ pyenv rehash pyenv global 设置全局的 Python 版本,通过将版本号写入 ~/.pyenv/version 文件的方式。 $ pyenv global 3.4.0 pyenv local 设置面向程序的本地版本,通过将版本号写入当前目录下的 .python-version 文件的方式。通过这种方式设置的 Python 版本优先级较 global 高。 pyenv会从当前目录开始向上逐级查找 .python-version 文件,直到根目录为止。若找不到,就用 global 版本。 $ pyenv local 3.4.2 4.jupyter ipython web页面版,具体使用可自己摸索 pip install ipython pip install jupyter jupyter notebook --ip=0.0.0.0 5.建立自己的隔离环境 pyenv virtualenv 3.4.2 eddy_001 pyenv local eddy_001 5.pymysql模块 连接数据库:conn = pymysql.connect(host='192.168.6.229',port=3306,user='mon',passwd='123456',db='pymysql_test',charset='utf8') 获取操作游标:curs=conn.cursor() 执行sql语句,插入记录:sta=curs.execute("insert 语句") 执行成功后sta值为1。更新、删除语句与此类似 执行sql语句,查询记录:cur.execute("select语句") 执行成功后cur变量中保存了查询结果记录集,然后再用循环打印结果: for row in curs: for col in row: print(col.decode('utf-8'), end = ' ')#如果是中文则需要处理编码 print() 关闭数据库连接: curs.close(); conn.close(); #!/root/.pyenv/shims/python #-*- coding: utf-8 -*- #@author: eddy #@contact: [email protected] #@site: http://my.oschina.net/eddylinux import pymysql #连接数据库函数 def connDB(): conn = pymysql.connect(host='X.X.X.X',port=3306,user='XXX',passwd='XXXXXX',db='XXXXX',charset='utf8') curs = conn.cursor() return(conn, curs) #更新语句,可执行update,insert语句 #sta=cur.execute("insert 语句") 执行成功后sta值为1。更新、删除语句与此类似 def exeUdate(curs,sql): sta = curs.execute(sql) return(sta) #删除语句,可批量删除 def exeDelete(curs,IDs): for eachID in IDs.split(','): sta=curs.execute('delete from eddy where id =%d' %int(eachID)) return(sta) #查询语句 def exeQuery(cur,sql): curs.execute(sql) return (cur) #关闭所有连接 def connClose(conn,curs): curs.close() conn.close() #调用连接数据库的函数 conn,curs = connDB() #调用更新记录的函数 sta = exeUdate(curs,'insert into eddy values(1,"eddy1")') if sta == 1: print('insert successed') else: print('insert failed') #查询现有数据,并打印 exeQuery(curs, 'SELECT * FROM eddy') for row in curs: for col in row: print(col, end = ' ') print() #批量删除记录,用户输入要删除的记录id号 ID=input('请输入要删除的编号,编号之间用逗号分开:'); sta=exeDelete(curs, ID); if(sta==1): print('删除成功'); else: print('删除失败'); #关闭数据库 connClose(conn, curs) paramiko安装 pip install paramiko #!/root/.pyenv/shims/python #-*- coding: utf-8 -*- #@author: eddy #@contact: [email protected] #@site: http://my.oschina.net/eddylinux import paramiko #设置基本变量主机,端口,用户名,密码,命令 HOST = "192.168.6.229" PORT = 22 USER = "root" PASSWD = "123456" CMD = "ifconfig" ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #传递基本变量到连接信息中 ssh.connect(HOST,PORT,USER,PASSWD) #执行命令,并把结果付给变量 stdin,stdout,stderr = ssh.exec_command(CMD) result = stdout print("*" * 20 ,"结果","*" * 20) for i in result: print(i, end = ' ') #上传、下载 t = paramiko.Transport((HOST,PORT)) t.connect(username=USER,password=PASSWD) sftp = paramiko.SFTPClient.from_transport(t) sftp.put('/tmp/test.py','/tmp/test.py') sftp.get('/tmp/check_web.py','/tmp/check_web.py') t.close() CMD = 'ls /tmp/test.py' stdin,stdout,stderr = ssh.exec_command(CMD) result = stdout print("*" * 20 ,"结果","*" * 20) for i in result: print(i, end = ' ') ssh.close() 执行结果 ******************** 结果 ******************** eth0 Link encap:Ethernet HWaddr 00:0C:29:83:AF:C8 inet addr:192.168.6.229 Bcast:192.168.6.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe83:afc8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:96089284 errors:0 dropped:0 overruns:0 frame:0 TX packets:52108810 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:13832749831 (12.8 GiB) TX bytes:4450847967 (4.1 GiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:48058661 errors:0 dropped:0 overruns:0 frame:0 TX packets:48058661 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:10916592053 (10.1 GiB) TX bytes:10916592053 (10.1 GiB) ******************** 结果 ******************** /tmp/test.py 附:pymysql简要说明 Contents Requirements Installation Test Suite Example Resources License This package contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb and work on CPython, PyPy and IronPython. Requirements Python -- one of the following: CPython >= 2.6 or >= 3.3 PyPy >= 4.0 IronPython 2.7 MySQL Server -- one of the following: MySQL >= 4.1 (tested with only 5.5~) MariaDB >= 5.1 Installation The last stable release is available on PyPI and can be installed with pip: $ pip install PyMySQL Alternatively (e.g. if pip is not available), a tarball can be downloaded from GitHub and installed with Setuptools: $ # X.X is the desired PyMySQL version (e.g. 0.5 or 0.6). $ curl -L https://github.com/PyMySQL/PyMySQL/tarball/pymysql-X.X | tar xz $ cd PyMySQL* $ python setup.py install $ # The folder PyMySQL* can be safely removed now. Test Suite If you would like to run the test suite, create database for test like this: mysql -e 'create database test_pymysql DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;' mysql -e 'create database test_pymysql2 DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;' Then, copy the file .travis.databases.json to pymysql/tests/databases.json and edit the new file to match your MySQL configuration: $ cp .travis.databases.json pymysql/tests/databases.json $ $EDITOR pymysql/tests/databases.json To run all the tests, execute the script runtests.py: $ python runtests.py A tox.ini file is also provided for conveniently running tests on multiple Python versions: $ tox Example CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(255) COLLATE utf8_bin NOT NULL, `password` varchar(255) COLLATE utf8_bin NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_binAUTO_INCREMENT=1 ; import pymysql.cursors# Connect to the databaseconnection = pymysql.connect(host='localhost', user='user', password='passwd', db='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, ('[email protected]', '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, ('[email protected]',)) result = cursor.fetchone() print(result)finally: connection.close() This example will print: {'password': 'very-secret', 'id': 1} Resources DownLoad:https://pypi.python.org/pypi/PyMySQL DB-API 2.0: http://www.python.org/dev/peps/pep-0249 MySQL Reference Manuals: http://dev.mysql.com/doc/ MySQL client/server protocol: http://dev.mysql.com/doc/internals/en/client-server-protocol.html PyMySQL mailing list: https://groups.google.com/forum/#!forum/pymysql-users paramiko: API docs:http://docs.paramiko.org