《Python 数据库 GUI CGI编程》
Posted wozhuzaisi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《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 模块,否则会报出来模块找不到。
使用包安装可以 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
![](http://upload-images.jianshu.io/upload_images/3117070-d5cbe56c750a86a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
$ pip install MySQL-Python
![](http://upload-images.jianshu.io/upload_images/3117070-8f1bd18524e21412.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.3 查询数据库
代码在GitHub上的源码地址:http://t.cn/RQjcvfl
![](http://upload-images.jianshu.io/upload_images/3117070-72ef2db8ffcd54e4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/3117070-1749a74a4dc96271.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3. GUI编程
Python 提供了多个图形开发界面的库,其中Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。
代码在GitHub上的源码地址:http://t.cn/RQjcvfl
![](http://upload-images.jianshu.io/upload_images/3117070-dae0b2c8bf1655b6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/3117070-5833de1a49a6ea6c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4. CGI编程
1)Python的CGIHTTPServer一般是与python一起安装的,使用如下命令既可以启动,为了便于组织目录,建议先建立一个目录,比如web,然后再运行下面的命令。
nohup python -m CGIHTTPServer 8088 &
![](http://upload-images.jianshu.io/upload_images/3117070-00cc1af008cb3c8a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2)静态文件代码地址:http://t.cn/R8wMYHr
![](http://upload-images.jianshu.io/upload_images/3117070-9e4e8ec8573a0254.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/3117070-33e1c45d2af3be33.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3)提交给后端的地址是 http://localhost:8088/cgi-bin/c_area.sh
![](http://upload-images.jianshu.io/upload_images/3117070-e994ba2a41bae758.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
后端的shell脚本地址:http://t.cn/R8wMmMn
![](http://upload-images.jianshu.io/upload_images/3117070-bbf184ce3cd03c4a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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 一图胜千言
![](http://upload-images.jianshu.io/upload_images/3117070-e1bf1ee7d3208145.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以升级的自己的python版本,建议直接采用Python3,毕竟是趋势。使用 “2to3工具”也是不错的选择。
6. 写在最后
本文简单接受了Python的数据库链接,GUI, CUI的简单编程,最后简单介绍了Python2 与Python3的区别,欢迎大家交流。
![](http://upload-images.jianshu.io/upload_images/3117070-8aa238b02d1b8523.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
以上是关于《Python 数据库 GUI CGI编程》的主要内容,如果未能解决你的问题,请参考以下文章