Warning: curl_exec() has been disabled for security reasons in /pub/host/sunboyu/sunboyu/www/wp-includes/http.php on line 1022

Warning: Cannot modify header information - headers already sent by (output started at /pub/host/sunboyu/sunboyu/www/wp-includes/http.php:1022) in /pub/host/sunboyu/sunboyu/www/wp-includes/feed-rss2.php on line 8
一个程序猿 » Python http://www.sunboyu.cn 时光不会倒流,脚步总要前进 Tue, 31 Jan 2012 10:50:34 +0000 http://wordpress.org/?v=2.7 en hourly 1 django笔记3-DEMO篇 http://www.sunboyu.cn/2009/12/15/django%e7%ac%94%e8%ae%b03-demo%e7%af%87.shtml http://www.sunboyu.cn/2009/12/15/django%e7%ac%94%e8%ae%b03-demo%e7%af%87.shtml#comments Tue, 15 Dec 2009 13:06:11 +0000 admin http://www.sunboyu.cn/?p=1089 1、创建一个project(可理解为站点)

django-admin.py startproject project1

发现新建了一个文件夹 project1

2、创建一个app(可理解为一个……)

python manage.py app1

发现多了一个文件夹 app1

3 、vi ./app1/views.py 增加代码

  1. from django.http import HttpResponse
  2. def index(self,request):
  3.     return HttpResponse('hello test')

4、vi ./urls.py 增加代码
( r’^tests/’ , ‘project1.app1.views.index’ ),

5、启动服务

python manage.py runserver domain.com:8000

然后在浏览器打 domain.com:8000/tests

如果能看到 hello test则证明配置成功。

如果不成功,看debug信息吧,debug默认是开启的。

另外我自己配置使用fastcgi方式运行python,python manage.py runfcgi host=127.0.0.1 port=8000,然后用nginx代理访问。两种方式还有所不同,具体的不同点暂时还不知道,希望知道这些差别的大大们多加提示,继续研究中。

]]>
http://www.sunboyu.cn/2009/12/15/django%e7%ac%94%e8%ae%b03-demo%e7%af%87.shtml/feed
django笔记1-安装篇 http://www.sunboyu.cn/2009/12/14/django%e7%ac%94%e8%ae%b01-%e5%ae%89%e8%a3%85%e7%af%87.shtml http://www.sunboyu.cn/2009/12/14/django%e7%ac%94%e8%ae%b01-%e5%ae%89%e8%a3%85%e7%af%87.shtml#comments Mon, 14 Dec 2009 11:02:47 +0000 admin http://www.sunboyu.cn/?p=1082 1、安装django,当然要安装python,我安装的python2.5

./configure –prefix=你的路径

2、安装mysqldb

这个可以看这篇文章 http://www.sunboyu.cn/2009/04/22/python25mysqldb122%E5%AE%89%E8%A3%85.shtml

3、安装easl_install

http://pypi.python.org/pypi/setuptools 我下的源码,按照提示安装就行

4、使用easl_install安装flup

地址 http://www.saddi.com/software/flup/dist/flup-1.0.2-py2.5.egg

5、安装django1.1

python setup install

到这里大体就算安装完了,底下配置。

]]>
http://www.sunboyu.cn/2009/12/14/django%e7%ac%94%e8%ae%b01-%e5%ae%89%e8%a3%85%e7%af%87.shtml/feed
设计标准的通讯协议 http://www.sunboyu.cn/2009/05/17/%e8%ae%be%e8%ae%a1%e6%a0%87%e5%87%86%e7%9a%84%e9%80%9a%e8%ae%af%e5%8d%8f%e8%ae%ae.shtml http://www.sunboyu.cn/2009/05/17/%e8%ae%be%e8%ae%a1%e6%a0%87%e5%87%86%e7%9a%84%e9%80%9a%e8%ae%af%e5%8d%8f%e8%ae%ae.shtml#comments Sun, 17 May 2009 15:48:25 +0000 admin http://www.sunboyu.cn/?p=710 当然不会是底层通讯协议,因为TCP/IP已经为我们准备好了足够完善的通讯机制确保稳定安全。

此协议为七层应用协议,跟http ftp是平级的,使用socket进行通讯,可以兼容php、python、java、c等语言。

协议开源,使用点对点信息校验,满足普通应用。

协议模拟tcp封包过程,进行数据封装。

协议内容:(伪代码)

  1. struct vhost
  2. {
  3.     Head varchar(10),            //协议头
  4.     Version int(5),                 //版本
  5.     Timestamp int(5),            //时间戳
  6.     Length int(10),                //包长度
  7.     Signature varchar(10),      //签名
  8.     Date varchar(500)            //数据
  9. }

协议包包括了协议头,版本,时间戳,包长度、签名(防止数据篡改和伪造),数据。基本能满足应用。

控制端数据打包发送后,受控端只需返回接受成功即可,以便客户端及时作出判断。

底下针对此包做PHP版本的封包和python的解包部分。

注释:原来使用问答的方式进行通讯,需要多次数据的应答,而此次的修改只需一次即可完成,而打包封包在一端完成,这样对网络稳定性的依赖就会降低。

]]>
http://www.sunboyu.cn/2009/05/17/%e8%ae%be%e8%ae%a1%e6%a0%87%e5%87%86%e7%9a%84%e9%80%9a%e8%ae%af%e5%8d%8f%e8%ae%ae.shtml/feed
Python写的数据库备份程序 http://www.sunboyu.cn/2009/04/22/python%e5%86%99%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e5%a4%87%e4%bb%bd%e7%a8%8b%e5%ba%8f.shtml http://www.sunboyu.cn/2009/04/22/python%e5%86%99%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e5%a4%87%e4%bb%bd%e7%a8%8b%e5%ba%8f.shtml#comments Tue, 21 Apr 2009 17:55:00 +0000 admin http://www.sunboyu.cn/?p=664 下载地址:mysqlbak

  1. #!/var/opt/python-2.5/bin/python
  2. import os
  3. import time
  4. import MySQLdb
  5.  
  6.  
  7. CFG = {}
  8. CFG['root'] = {}
  9. CFG['root']['hostname'] = 'localhost'
  10. CFG['root']['username'] = 'root'
  11. CFG['root']['password'] = 'test'
  12.  
  13.  
  14. CFG['mysqldump'] = '/opt/mysql-5.0.22/bin/mysqldump'
  15.  
  16. CFG['time'] = time.strftime("%Y-%m-%d", time.localtime(time.time()) )
  17.  
  18. CFG['bakpath'] = '/tmp/' + CFG['time']
  19. CFG['mainbak'] = '/home/sunboyu/databak/' + CFG['time']
  20.  
  21.  
  22. if os.path.exists( CFG['bakpath'] ) == False:
  23.     os.mkdir( CFG['bakpath'] )
  24. if os.path.exists( CFG['mainbak'] ) == False:
  25.     os.mkdir( CFG['mainbak'] )
  26.  
  27. def mysql_database_bak( database ):
  28.     global CFG
  29.     DIR = CFG['bakpath'] + "/" + database
  30.     if os.path.exists( DIR )==False:
  31.         os.mkdir( DIR )
  32.     conn = MySQLdb.connect( host = CFG['root']['hostname'] , user = CFG['root']['username'] , passwd = CFG['root']['password'] , db = database )
  33.     db = conn.cursor()
  34.     sql = "show tables";
  35.     db.execute( sql )
  36.     result = {}
  37.     for recordline in db.fetchall():
  38.         os.system( CFG['mysqldump'] + " --opt  " + database + " " + recordline[0] + " -u" + CFG['root']['username'] + " -p" + CFG['root']['password'] + " > " + DIR + "/" + database + "_" + recordline[0] + ".sql" )
  39.     os.system( "tar cvf " + CFG['bakpath'] + "/" + database + ".tar.gz " +  CFG['bakpath'] + "/" + database )
  40.     os.system( "mv " + CFG['bakpath'] + "/" + database + ".tar.gz " + CFG['mainbak'] + '/' )
  41.  
  42.  
  43. conn = MySQLdb.connect( host = CFG['root']['hostname'] , user = CFG['root']['username'] , passwd = CFG['root']['password'] , db = 'test' )
  44. sql = "show databases";
  45. db = conn.cursor()
  46. db.execute( sql )
  47. result = {}
  48. bigcount = 0
  49. for recordline in db.fetchall():
  50. littlecount = 0
  51. result[bigcount] = {}
  52. for colnum in db.description:
  53.     result[bigcount][colnum[0]] = recordline[littlecount]
  54.     littlecount += 1
  55. bigcount += 1
  56.  
  57.  
  58. for tables in result:
  59.     mysql_database_bak( result[tables]['Database'] )
  60.  
  61. os.system( "rm -rf /tmp/" + CFG['time'] )  #警告 rm -rf 这个命令相当危险,使用一定要谨慎
]]>
http://www.sunboyu.cn/2009/04/22/python%e5%86%99%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e5%a4%87%e4%bb%bd%e7%a8%8b%e5%ba%8f.shtml/feed
python2.5+MySQLdb1.2.2安装 http://www.sunboyu.cn/2009/04/22/python25mysqldb122%e5%ae%89%e8%a3%85.shtml http://www.sunboyu.cn/2009/04/22/python25mysqldb122%e5%ae%89%e8%a3%85.shtml#comments Tue, 21 Apr 2009 16:04:12 +0000 admin http://www.sunboyu.cn/?p=662 最近比较喜欢用python写一些shell,又因为要跟mysql交互,所以安装python2.5+MySQLdb1.2.2。

选择python而没有选择perl,c之类,是因为python语法相对简单,适合我。其实php也可以,但linux默认安装python并大量使用,而并不默认安装PHP。

在安装过程中遇到很多问题,暂不罗列,google是半万能的,多尝试。

使用这两个版本,是因为在编译过程中的问题,逼我仔细阅读了产品稳当,发现版本依赖性很强,最终选择的这两个。

python编译很简单 ./configure –prefix=/***** 就OK了。

MySQLdb的安装也很简单,但首先要修改site.cfg的参数,其中threadsafe和mysql_config的值要根据情况修改。

python setup.py build
(如果必要,中间运行这个 ln -s /opt/mysql-5.0.22/lib/libmysqlclient.a ./build/lib.linux-x86_64-2.4/_mysql.so 路径自己调整)
python setup.py install

]]>
http://www.sunboyu.cn/2009/04/22/python25mysqldb122%e5%ae%89%e8%a3%85.shtml/feed
Python写的数据抽象层 http://www.sunboyu.cn/2009/03/30/python%e5%86%99%e7%9a%84%e6%95%b0%e6%8d%ae%e6%8a%bd%e8%b1%a1%e5%b1%82.shtml http://www.sunboyu.cn/2009/03/30/python%e5%86%99%e7%9a%84%e6%95%b0%e6%8d%ae%e6%8a%bd%e8%b1%a1%e5%b1%82.shtml#comments Mon, 30 Mar 2009 03:26:09 +0000 admin http://www.sunboyu.cn/?p=622 python本身没有对mysql的支持,得依赖附加的库 http://mysql-python.sourceforge.net 。很多框架采用了这个库,比如adodb,django等。不过我倾向于自己写这些东西,所以小研究了一下。 mysql-python 有两个对象 MySQLdb _mysql 这里是介绍http://mysql-python.sourceforge.net/MySQLdb.html,我使用的是 MySQLdb 对象

  1. #!/D:\python25\python.exe
  2. import MySQLdb
  3.  
  4. class Mysql(object):
  5.     handle = ''
  6.     query = ''
  7.     exectime = 0
  8.     effected = 0
  9.  
  10.     def __init__( self , hostname , username , password , database ):
  11.         db = MySQLdb.connect( host = hostname , user = username , passwd = password , db = database )
  12.         self.handle = db.cursor()
  13.         
  14.     def query( self , sql = '' ):
  15.         return self.handle.execute( sql )
  16.  
  17.     def fetch( self , sql = '' ):
  18.         self.handle.execute( sql )
  19.         rs = self.handle.fetchone()
  20.         count = 0
  21.         result = {}
  22.         for colnum in self.handle.description:
  23.             result[colnum[0]] = rs[count]
  24.             count += 1
  25.         return result
  26.     
  27.     def fetchAll( self , sql = '' ):
  28.         self.handle.execute( sql )
  29.         result = {}
  30.         bigcount = 0
  31.         for recordline in self.handle.fetchall():
  32.             littlecount = 0
  33.             result[bigcount] = {}
  34.             for colnum in self.handle.description:
  35.                 result[bigcount][colnum[0]] = recordline[littlecount]
  36.                 littlecount += 1
  37.             bigcount += 1
  38.         return result
  39.  
  40.     def insertID( self ):
  41.           self.handle.execute("SELECT LAST_INSERT_ID() AS lid")
  42.           rs = self.handle.fetchone()
  43.           return rs[0]
  44.  
  45.     def close( self ):
  46.           self.handle.close()
  47.           pass
  48. #DEMO
  49. db = Mysql('localhost','root','123456','frame')
  50. db.fetchAll('select * from user')
  51. #rs = db.fetchAll('select * from user')
  52. #sql = "INSERT INTO tags (module,fid,name,note,`order`) VALUE ('test',1,'fd','ds',1)"
  53. #db.query(sql)
  54. #print db.insertID()
]]>
http://www.sunboyu.cn/2009/03/30/python%e5%86%99%e7%9a%84%e6%95%b0%e6%8d%ae%e6%8a%bd%e8%b1%a1%e5%b1%82.shtml/feed
解决了该死的权限问题,是否真的有效 http://www.sunboyu.cn/2009/03/12/%e8%a7%a3%e5%86%b3%e4%ba%86%e8%af%a5%e6%ad%bb%e7%9a%84%e6%9d%83%e9%99%90%e9%97%ae%e9%a2%98%ef%bc%8c%e6%98%af%e5%90%a6%e7%9c%9f%e7%9a%84%e6%9c%89%e6%95%88.shtml http://www.sunboyu.cn/2009/03/12/%e8%a7%a3%e5%86%b3%e4%ba%86%e8%af%a5%e6%ad%bb%e7%9a%84%e6%9d%83%e9%99%90%e9%97%ae%e9%a2%98%ef%bc%8c%e6%98%af%e5%90%a6%e7%9c%9f%e7%9a%84%e6%9c%89%e6%95%88.shtml#comments Thu, 12 Mar 2009 15:43:34 +0000 admin http://www.sunboyu.cn/?p=600 我的apache+php权限是配置的最为严格的,当然,在用的时候难免伴随着混乱的账户情况,linux的权限机制也是很让人头疼的,终于,还是在风平浪静种碰到了麻烦。

我不认为PHP很强,因为它只是一个面向Web的脚本语言,而PHP的开发者却赋予了它太多,让人去用,有人也滥用。

当然,一门语言能解决N多问题是好的,比如汇编,C,但终究PHP有它跨不过的坎。毕竟它只是web脚本语言。

权限出现很大的问题,至今没有搞透,说白了对linux还是一知半解,使用python写了个第三方的东西,很完美得跳跃了权限的问题。至于效率,python肯定要比php强的,起码PHP作为服务器端程序运行,PHP还没有线程和进程的控制(一直没有发现),python有完善的线程进程的库。在权限管理上,python没细看,PHP在linux下有posix函数库,我一直也没有用过。

在没有更好的解决方案前,我依然用PHP做服务器端程序,python作为一些补充。也许,全部切换过去。

]]>
http://www.sunboyu.cn/2009/03/12/%e8%a7%a3%e5%86%b3%e4%ba%86%e8%af%a5%e6%ad%bb%e7%9a%84%e6%9d%83%e9%99%90%e9%97%ae%e9%a2%98%ef%bc%8c%e6%98%af%e5%90%a6%e7%9c%9f%e7%9a%84%e6%9c%89%e6%95%88.shtml/feed
使用PYTHON小提apache下的PHP执行权限 http://www.sunboyu.cn/2009/02/28/%e4%bd%bf%e7%94%a8python%e5%b0%8f%e6%8f%90apache%e4%b8%8b%e7%9a%84php%e6%89%a7%e8%a1%8c%e6%9d%83%e9%99%90.shtml http://www.sunboyu.cn/2009/02/28/%e4%bd%bf%e7%94%a8python%e5%b0%8f%e6%8f%90apache%e4%b8%8b%e7%9a%84php%e6%89%a7%e8%a1%8c%e6%9d%83%e9%99%90.shtml#comments Sat, 28 Feb 2009 15:15:13 +0000 admin http://www.sunboyu.cn/?p=583 绕了个圈,权限彻底上去了,中间用socket通讯方式解决。
代码暂时还不安全,等拿出安全的方案后公布

]]>
http://www.sunboyu.cn/2009/02/28/%e4%bd%bf%e7%94%a8python%e5%b0%8f%e6%8f%90apache%e4%b8%8b%e7%9a%84php%e6%89%a7%e8%a1%8c%e6%9d%83%e9%99%90.shtml/feed
Python编译参数 http://www.sunboyu.cn/2009/02/21/python%e7%bc%96%e8%af%91%e5%8f%82%e6%95%b0.shtml http://www.sunboyu.cn/2009/02/21/python%e7%bc%96%e8%af%91%e5%8f%82%e6%95%b0.shtml#comments Sat, 21 Feb 2009 15:38:21 +0000 admin http://www.sunboyu.cn/?p=573 python2.6.1和mod_python的编译参数

  1. #python编译参数
  2. ./configure --prefix=/opt/python-2.6.1 \
  3. --enable-shared \
  4. --enable-profiling \
  5. --with-gcc \
  6. --with-pydebug \
  7. --with-system-ffi \
  8. --with-signal-module \
  9. --with-dec-threads \
  10. --with-threads \
  11. --with-thread \
  12. --with-pth \
  13. --with-doc-strings \
  14. --with-tsc \
  15. --with-pymalloc \
  16. --with-fpectl
  17.  
  18. #python_mod编译参数
  19. ./configure --with-apxs=/opt/httpd-2.2.9 \
  20. --with-python=/opt/python-2.6.1
]]>
http://www.sunboyu.cn/2009/02/21/python%e7%bc%96%e8%af%91%e5%8f%82%e6%95%b0.shtml/feed
明确了方向,Gentoo,Python http://www.sunboyu.cn/2009/01/17/%e6%98%8e%e7%a1%ae%e4%ba%86%e6%96%b9%e5%90%91gentoopython.shtml http://www.sunboyu.cn/2009/01/17/%e6%98%8e%e7%a1%ae%e4%ba%86%e6%96%b9%e5%90%91gentoopython.shtml#comments Fri, 16 Jan 2009 16:03:47 +0000 admin http://www.sunboyu.cn/?p=493 晚上收拾东西,翻出了很多的Linux发行版,Ubuntu,SuseLinux,红旗,Fedora等。不过我最终还是留下了Centos的几个版本。

Fedora5是我的第一个实际应用的发行版,因为公司当时从win迁移linux平台,红帽是我曾经听说过的系统,简单实践过,就硬上了,好在有yum这东西,很端的时间把服务都配起来,还真跑起来了。

不过Fedora在DELL1950的机器上有个硬伤,就是对双网卡的支持不好,只能支持一块网卡,后来迫不得已,找到了Centos,果然很爽,就一直在用了。

以后,基本一直在研究和优化linux下的Web相关服务。不断优化,改进,定制。

终于发现在发行版的基础上优化和精简,逐渐有了障碍,我需要研究linux的各种服务的原理,与其去分析每个服务,不如从零配置一个服务。两个方案,LFS和gentoo。

LFS似乎是个很神秘的东西,其实现在的资料很全,而且很多高手给出了详细的流程。但我选择了gentoo,因为gentoo是完全根据用户定制进行安装,是在一个稳定的基础上进行定制,定制的产品也许稳定性和效率会很高。而LFS,如果我对linux的理解不是太深,也许作出来的版本也是不能进行生产的,只能算个实验室产品。

因此,本着劳以致用的原则,我开始对gentoo系统的研究,另外继续发展熟悉的CentOS4.7Server版本用作生产。

希望下年我能完全切换到gentoo环境+python开发。

注:转移到python,是因为最近我被一个PHP进程效率问题折腾的焦头烂额,而在linux上实现多个php进程调度的困难和效率的损失让我彻底对PHP失去了信心,因为PHP就是一个面向Web的开发语言,用迷信崇拜的思想往其他领域迁移实在是难受,因此用python去解决多进程的问题再好不好,python的解释型语言和面向对象特性跟PHP很是相似,但相关的类库却接近于底层,又没有c的完全面向底层。python也许又是我下一个迷信的东西。

]]>
http://www.sunboyu.cn/2009/01/17/%e6%98%8e%e7%a1%ae%e4%ba%86%e6%96%b9%e5%90%91gentoopython.shtml/feed
新书,不是一般的新 http://www.sunboyu.cn/2008/12/12/%e6%96%b0%e4%b9%a6%ef%bc%8c%e4%b8%8d%e6%98%af%e4%b8%80%e8%88%ac%e7%9a%84%e6%96%b0.shtml http://www.sunboyu.cn/2008/12/12/%e6%96%b0%e4%b9%a6%ef%bc%8c%e4%b8%8d%e6%98%af%e4%b8%80%e8%88%ac%e7%9a%84%e6%96%b0.shtml#comments Fri, 12 Dec 2008 03:34:03 +0000 admin http://www.sunboyu.cn/?p=423

中午到中关村图书大厦购得,不错。

]]>
http://www.sunboyu.cn/2008/12/12/%e6%96%b0%e4%b9%a6%ef%bc%8c%e4%b8%8d%e6%98%af%e4%b8%80%e8%88%ac%e7%9a%84%e6%96%b0.shtml/feed
自行车VS Program http://www.sunboyu.cn/2008/12/12/%e8%87%aa%e8%a1%8c%e8%bd%a6vs-program.shtml http://www.sunboyu.cn/2008/12/12/%e8%87%aa%e8%a1%8c%e8%bd%a6vs-program.shtml#comments Fri, 12 Dec 2008 01:02:07 +0000 admin http://www.sunboyu.cn/?p=419

]]>
http://www.sunboyu.cn/2008/12/12/%e8%87%aa%e8%a1%8c%e8%bd%a6vs-program.shtml/feed
购得一本新书:《Python网络编程基础》 http://www.sunboyu.cn/2008/12/08/%e8%b4%ad%e5%be%97%e4%b8%80%e6%9c%ac%e6%96%b0%e4%b9%a6%ef%bc%9a%e3%80%8apython%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80%e3%80%8b.shtml http://www.sunboyu.cn/2008/12/08/%e8%b4%ad%e5%be%97%e4%b8%80%e6%9c%ac%e6%96%b0%e4%b9%a6%ef%bc%9a%e3%80%8apython%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80%e3%80%8b.shtml#comments Mon, 08 Dec 2008 15:04:44 +0000 admin http://www.sunboyu.cn/?p=416 过分迷信PHP是不行的,做事还得找专长

]]>
http://www.sunboyu.cn/2008/12/08/%e8%b4%ad%e5%be%97%e4%b8%80%e6%9c%ac%e6%96%b0%e4%b9%a6%ef%bc%9a%e3%80%8apython%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80%e3%80%8b.shtml/feed