wall.py

来自「Ajax最流行书籍」· Python 代码 · 共 91 行

PY
91
字号
#!/usr/bin/env pythonimport cgiimport cgitb;cgitb.enable()import datetimeimport osimport sajax1WALLFILE = '/tmp/wall.html'if not os.path.exists(WALLFILE):   fh = open(WALLFILE, 'w')   fh.close()def colourify_ip(ip):   colour = ''.join(['%02x' % int(part) for part in ip.split('.')[-3:]])   return colour   def add_line(msg):   f = open("/tmp/wall.html","a")   dt = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")   msg = cgi.escape(msg)   remote = os.environ['REMOTE_ADDR']   colour = colourify_ip(remote)   f.write('<span style="color:#%(colour)s">%(dt)s</span> %(msg)s<br />\n' % locals())   f.close()   def refresh():   f = open("/tmp/wall.html")   return '\n'.join(list(f)[-25:])   sajax1.sajax_init()sajax1.sajax_export(refresh, add_line)sajax1.sajax_handle_client_request()print """<html><head>	<title>PyWall</title>	<script>"""sajax1.sajax_show_javascript()print """	var check_n = 0;		function refresh_cb(new_data) {		document.getElementById("wall").innerHTML = new_data;		document.getElementById("status").innerHTML = "Checked #" + check_n++;		setTimeout("refresh()", 1000);	}		function refresh() {		document.getElementById("status").innerHTML = "Checking..";		x_refresh(refresh_cb);	}		function add_cb() {		// we don't care..	}	function add() {		var line;		var handle;		handle = document.getElementById("handle").value;		line = document.getElementById("line").value;		if (line == "") 			return;		x_add_line("[" + handle + "] " + line, add_cb);		document.getElementById("line").value = "";	}	</script>	</head><body onload="refresh();">	<a href="http://">Sajax</a> - Wall Example<br/>		<input type="text" name="handle" id="handle" value="(name)"		onfocus="this.select()" style="width:130px;">	<input type="text" name="line" id="line" value="(enter your message here)"		onfocus="this.select()"		style="width:300px;">	<input type="button" name="check" value="Post message"		onclick="add(); return false;">	<div id="wall"></div>	<div id="status"><em>Loading..</em></div>	</body></html>""" % locals()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?