Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | #!/usr/bin/env python |
2 | import cgi |
||
3 | import cgitb;cgitb.enable() |
||
4 | import datetime |
||
5 | import os |
||
6 | |||
7 | import sajax1 |
||
8 | |||
9 | WALLFILE = '/tmp/wall.html' |
||
10 | |||
11 | if not os.path.exists(WALLFILE): |
||
12 | fh = open(WALLFILE, 'w') |
||
13 | fh.close() |
||
14 | |||
15 | def colourify_ip(ip): |
||
16 | colour = ''.join(['%02x' % int(part) for part in ip.split('.')[-3:]]) |
||
17 | return colour |
||
18 | |||
19 | def add_line(msg): |
||
20 | f = open("/tmp/wall.html","a") |
||
21 | dt = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") |
||
22 | msg = cgi.escape(msg) |
||
23 | remote = os.environ['REMOTE_ADDR'] |
||
24 | colour = colourify_ip(remote) |
||
25 | f.write('<span style="color:#%(colour)s">%(dt)s</span> %(msg)s<br />\n' % locals()) |
||
26 | f.close() |
||
27 | |||
28 | def refresh(): |
||
29 | f = open("/tmp/wall.html") |
||
30 | return '\n'.join(list(f)[-25:]) |
||
31 | |||
32 | sajax1.sajax_init() |
||
33 | sajax1.sajax_export(refresh, add_line) |
||
34 | sajax1.sajax_handle_client_request() |
||
35 | |||
36 | print """ |
||
37 | <html> |
||
38 | <head> |
||
39 | <title>PyWall</title> |
||
40 | <script> |
||
41 | """ |
||
42 | sajax1.sajax_show_javascript() |
||
43 | print """ |
||
44 | var check_n = 0; |
||
45 | |||
46 | function refresh_cb(new_data) { |
||
47 | document.getElementById("wall").innerHTML = new_data; |
||
48 | document.getElementById("status").innerHTML = "Checked #" + check_n++; |
||
49 | setTimeout("refresh()", 1000); |
||
50 | } |
||
51 | |||
52 | function refresh() { |
||
53 | document.getElementById("status").innerHTML = "Checking.."; |
||
54 | x_refresh(refresh_cb); |
||
55 | } |
||
56 | |||
57 | function add_cb() { |
||
58 | // we don't care.. |
||
59 | } |
||
60 | |||
61 | function add() { |
||
62 | var line; |
||
63 | var handle; |
||
64 | handle = document.getElementById("handle").value; |
||
65 | line = document.getElementById("line").value; |
||
66 | if (line == "") |
||
67 | return; |
||
68 | x_add_line("[" + handle + "] " + line, add_cb); |
||
69 | document.getElementById("line").value = ""; |
||
70 | } |
||
71 | </script> |
||
72 | |||
73 | </head> |
||
74 | <body onload="refresh();"> |
||
75 | |||
76 | <a href="http://">Sajax</a> - Wall Example<br/> |
||
77 | |||
78 | <input type="text" name="handle" id="handle" value="(name)" |
||
79 | onfocus="this.select()" style="width:130px;"> |
||
80 | <input type="text" name="line" id="line" value="(enter your message here)" |
||
81 | onfocus="this.select()" |
||
82 | style="width:300px;"> |
||
83 | <input type="button" name="check" value="Post message" |
||
84 | onclick="add(); return false;"> |
||
85 | <div id="wall"></div> |
||
86 | <div id="status"><em>Loading..</em></div> |
||
87 | |||
88 | </body> |
||
89 | </html> |
||
90 | """ % locals() |