~ivle-dev/ivle/debian-packaging

« back to all changes in this revision

Viewing changes to ivle/chat.py

  • Committer: William Grant
  • Date: 2010-07-28 04:16:28 UTC
  • mfrom: (1099.1.727 trunk)
  • Revision ID: grantw@unimelb.edu.au-20100728041628-0msynobn2grjpjz4
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# Author: Thomas Conway
20
20
# Date:   5/2/2008
21
21
 
22
 
import cjson
 
22
try:
 
23
    import json
 
24
except ImportError:
 
25
    import simplejson as json
 
26
 
23
27
import cStringIO
24
28
import hashlib
25
29
import sys
99
103
 
100
104
            response = handler(content)
101
105
 
102
 
            send_netstring(conn, cjson.encode(response))
 
106
            send_netstring(conn, json.dumps(response))
103
107
 
104
108
            conn.close()
105
109
 
106
110
        except Terminate, t:
107
111
            # Try and send final response and then terminate
108
112
            if t.final_response:
109
 
                send_netstring(conn, cjson.encode(t.final_response))
 
113
                send_netstring(conn, json.dumps(t.final_response))
110
114
            conn.close()
111
115
            sys.exit(0)
112
116
        except Exception:
119
123
                "value": str(e_val),
120
124
                "traceback": tb_dump.getvalue()
121
125
            }
122
 
            send_netstring(conn, cjson.encode(json_exc))
 
126
            send_netstring(conn, json.dumps(json_exc))
123
127
            conn.close()
124
128
 
125
129
 
128
132
    sok.connect((host, port))
129
133
    sok.settimeout(SOCKETTIMEOUT)
130
134
 
131
 
    json = encode(msg, magic)
 
135
    out = encode(msg, magic)
132
136
 
133
 
    send_netstring(sok, json)
 
137
    send_netstring(sok, out)
134
138
    inp = recv_netstring(sok)
135
139
 
136
140
    sok.close()
137
141
 
138
142
    if decode:
139
 
        return cjson.decode(inp)
 
143
        return json.loads(inp)
140
144
    else:
141
145
        return inp
142
146
 
145
149
    string to attach a HMAC digest.
146
150
    """
147
151
    # XXX: Any reason that we double encode?
148
 
    content = cjson.encode(message)
 
152
    content = json.dumps(message)
149
153
 
150
154
    digest = hashlib.md5(content + magic).hexdigest()
151
155
    env = {'digest':digest,'content':content}
152
 
    json = cjson.encode(env)
153
 
 
154
 
    return json
 
156
    return json.dumps(env)
155
157
 
156
158
 
157
159
def decode(message, magic):
158
160
    """Takes a message with an attached HMAC digest and validates the message.
159
161
    """
160
 
    msg = cjson.decode(message)
 
162
    msg = json.loads(message)
161
163
 
162
164
    # Check that the message is valid
163
165
    digest = hashlib.md5(msg['content'] + magic).hexdigest()
164
166
    if msg['digest'] != digest:
165
167
        raise ProtocolError("HMAC digest is invalid")
166
 
    content = cjson.decode(msg['content'])
 
168
    content = json.loads(msg['content'])
167
169
 
168
170
    return content
169
171