六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 100|回复: 0

极端简单的,秉着切磋学习的精神贴上来,欢迎拍砖

[复制链接]

升级  40%

4

主题

4

主题

4

主题

童生

Rank: 1

积分
20
 楼主| 发表于 2013-2-7 20:32:05 | 显示全部楼层 |阅读模式
http://python99.freebsdhost.org/board.py

因为是在网上直接写的,我用的这个免费空间用的是fastcgi模式,一旦我程序写错了都是500错误,写起来非常费劲,程序虽然简陋,也磕磕绊绊花了一天的时间.其中在处理留言者提交的变量,就是post的username和msg变量,我找不到python里可以直接处理的模块.在php里好像直接用$_POST['username']和$_POST['msg']就可以了,python里好像不行,所以我用了正则好歹匹配上了,不过如果是复杂的留言,估计会出错.这里大家如果有更好的方法,欢迎贴出来切磋.
      
      
mysql数据库:
CREATE TABLE IF NOT EXISTS `msg` (  `id` bigint(12) NOT NULL AUTO_INCREMENT,  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,  `ip_address` varchar(16) COLLATE utf8_unicode_ci NOT NULL,  `data` text COLLATE utf8_unicode_ci NOT NULL,  `time` int(10) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=12 ;
phpmyadmin导入即可
      
      
文件源码:
#!/usr/local/python2/bin/python# -*- coding: utf-8 -*-import reimport timeimport urllibimport adodbimport MySQLdbdef myapp(environ, start_response):    #////////////////数据库初始化/////////////    conn = adodb.NewADOConnection('mysql')    conn.Connect('localhost','user','psssword','dbname')    #////////////////数据库初始化/////////////    conn.Execute("set names 'utf8'")        username = ""    data = ""    post = str(environ['wsgi.input'].read())    if len(post)>0:        p = re.compile('username=(.*?)&msg=(.*?)&submit')        urls = p.findall(post)        i = 0        for x in urls[0]:            if i==1:                data = urllib.unquote_plus(x)            else:                username = urllib.unquote_plus(x)            i += 1                conn.Execute("INSERT INTO `msg` ( `id`, `username`, `ip_address`, `data`, `time` ) VALUES (%s, %s, %s, %s, %s)", (None, MySQLdb.escape_string(username), MySQLdb.escape_string(environ['REMOTE_ADDR']), MySQLdb.escape_string(data), time.time()))        post_table = ''    res = conn.Execute('select * from msg order by time DESC')    #print cursor.fields[1]    while not res.EOF:        topic = res.GetRowAssoc(0)        post_table += '<br><table width=600 border=1 cellspacing=\'2\' cellpadding=\'2\'><tr><td width=100>用户名</td><td>'+str(topic['username'])+'</td></tr><tr><td>留言时间</td><td>'+str(time.strftime('%Y-%m-%d %X', time.localtime(topic['time'])))+'</td></tr><tr><td>留言内容</td><td>'+str(topic['data'])+'</td></tr><tr></tr></table>'        res.MoveNext()    res.Close()    start_response('200 OK', [('Content-Type', 'text/html')])    return ["<html><HEAD><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><TITLE>留言板</TITLE></HEAD><body><form action=\"./board.py\" method=\"post\"><table width=600 border=1 cellspacing=\"2\" cellpadding=\"2\"><tr><td>用户名</td><td><input type=\"text\" name=\"username\" size=\"20\"></td></tr><tr><td>留言内容</td><td><textarea name=\"msg\" rows=\"3\" cols=\"60\"></textarea></td></tr><tr><td colspan=2><input name=\"submit\" type=\"submit\" value=\"提交留言\"></td></tr></table></form></body></html>"+post_table] #HTTP返回    if __name__ == '__main__':    from flup.server.fcgi import WSGIServer    WSGIServer(myapp).run()
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表