-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrui.py
executable file
·138 lines (109 loc) · 2.84 KB
/
crui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
#
# Copyright (C) 2011 GeekGene, All Rights Reserved.
#
import sys
import cgi
form = cgi.FieldStorage()
import time
import sys, traceback
import os
import json
import socket
import select
def get_well(name, default=""):
val = form.getfirst(name)
if val is None or val == '':
val = default
return val
def my_formatException():
strs = traceback.format_exception(*(sys.exc_info()))
out = ""
for str in strs:
out += str.replace('\n','\r\n')
return out
def log( name, str ):
fn = "/tmp/crui_py_log.%s" % name
f = file(fn,"ab")
f.write(" "+str+"\n");
f.flush()
f.close()
def doSend(name, s, data):
dOut = ""
if not data is None:
log(name, "Sending: "+str(data))
s.send(data+"\n")
try:
good = True
t = time.time()
while good:
il,ol,el = select.select([s],[],[s],.1)
if len(dOut)>0:
good = False
if len(il)>0:
dOut += s.recv(4096)
if time.time()-t<3:
good = True
except:
raise
idx = len(dOut)-1
while True:
if idx<0:
break;
c = dOut[idx]
if c == ' ' or c == '\t' or c == '\f' or c == '\n' or c == '\r' or c == '>':
idx-=1;
else:
break;
dOut = dOut[:idx+1]
log(name, "Recieved: "+str(dOut))
return dOut
def main():
the_top = "Expires: Mon, 26 Jul 1997 05:00:00 GMT\n"
the_top += "Last-Modified: " + str(time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))) + '\n'
the_top += "Cache-Control: no-store, no-cache, must-revalidate\n"
the_top += "Cache-Control: post-check=0, pre-check=0\n"
the_top += "Pragma: no-cache\n"
the_top += '''Content-Type: text/plain
'''
try:
RemoteAddr = '[no address]'
UserAgent = '[no ua]'
if os.environ.has_key( 'REMOTE_ADDR' ):
RemoteAddr = os.environ['REMOTE_ADDR']
if os.environ.has_key( 'HTTP_USER_AGENT' ):
UserAgent = os.environ['HTTP_USER_AGENT']
username = get_well("username","no-one")
msg = get_well("msg","gc")
data = get_well("data",None)
log(username, "---\nCon From " + str(RemoteAddr) + ", " + str(UserAgent))
log(username, time.ctime())
log(username, username + " " + str(msg) + " " + str(data))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.connect(("208.78.103.116",3333))
s.settimeout(None)
doSend(username, s, None)
rsp = doSend(username, s, username)
if rsp.find("status")<0 or rsp.find("ok")<0:
raise Exception("Could not log in with username '"+username+"'")
if not (data is None):
msg += " " + data
dataOut = doSend(username, s, msg)
print the_top + dataOut
s.close()
log(username, "Completed Call\n---")
except BaseException, data:
try:
dataOut = [[
'error', [
repr(data),
my_formatException()
]
]]
dataOut = json.write( dataOut )
print the_top + dataOut
except:
print the_top + '[["error", [%s,""]]]' % ('"' + repr(data).replace('"','\\"') + '"')
if __name__ == "__main__":
main()