《Python 数据库 GUI CGI编程》
本文地址:http://www.cnblogs.com/aiweixiao/p/8390417.html
原文地址
点击关注微信公众号 wenyuqinghuai
1.写在前边
上一次,我们介绍了Python的入门的文章,今天我们就来介绍下Python的数据库,GUI, CGI的编程,以及Python 2.x 与Python 3.x的区别。
2.连接数据库
Python 标准数据库接口为 Python DB-API,MySQLdb 是用于Python链接Mysql数据库的接口。使用的时候,必须安装MySQLdb 模块,否则会报出来模块找不到。
2.1 Linux下安装MySQLdb模块
使用包安装可以 wget http://sourceforge.net/projects/mysql-python/files/latest/download
$ tar xfz MySQL-python-1.2.3.tar.gz
$ cd MySQL-python-1.2.3
$whereis mysql_config mysql_config: /usr/bin/mysql_config
$ vim site.cfg 修改mysql_config为mysql配置文件的路径 /usr/bin/mysql_config
还要修改 threadsafe = False
$ python setup.py build
$ sudo python setup.py install
2.2 mac下安装MySQLdb模块
使用easy_install pip命令安装pip
$ easy_install pip

mac安装pip
$ pip install MySQL-Python

pip安装MySQL-Python
2.3 查询数据库
代码在GitHub上的源码地址:http://t.cn/RQjcvfl

操作数据库

数据库的输出效果
3. GUI编程
Python 提供了多个图形开发界面的库,其中Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。
代码在GitHub上的源码地址:http://t.cn/RQjcvfl

Python的GUI编程

mac系统下GUI的效果
4. CGI编程
1)Python的CGIHTTPServer一般是与python一起安装的,使用如下命令既可以启动,为了便于组织目录,建议先建立一个目录,比如web,然后再运行下面的命令。
nohup python -m CGIHTTPServer 8088 &

原理图
2)静态文件代码地址:http://t.cn/R8wMYHr

前端html代码
访问地址:http://localhost:8088/

前端图
3)提交给后端的地址是 http://localhost:8088/cgi-bin/c_area.sh

后端shell代码
后端的shell脚本地址:http://t.cn/R8wMmMn

提交后计算结果
5. Python 2.x 与 Python 3.x区别
5.1 两者不兼容
Python的3??.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。
为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下相容。
许多针对早期Python版本设计的程式都无法在Python 3.0上正常执行。
为了照顾现有程序,Python 2.6作为一个过渡版本,基本使用了Python 2.x的语法和库,同时考虑了向Python 3.0的迁移,允许使用部分Python 3.0的语法与函数。
5.2 一图胜千言

Python2与Python3区别
可以升级的自己的python版本,建议直接采用Python3,毕竟是趋势。使用 “2to3工具”也是不错的选择。
6. 写在最后
本文简单接受了Python的数据库链接,GUI, CUI的简单编程,最后简单介绍了Python2 与Python3的区别,欢迎大家交流。

有前途的Python