88
90
req.write(cjson.encode({"host": host, "port": port, "magic": magic}))
90
92
def handle_chat(req):
91
req.throw_error(req.HTTP_NOT_IMPLEMENTED)
93
# The request *should* have the following four fields:
94
# host, port: Host and port where the console server apparently lives
95
# digest, text: Fields to pass along to the console server
96
# It simply acts as a proxy to the console server
97
fields = req.get_fieldstorage()
99
host = fields.getfirst("host").value
100
port = fields.getfirst("port").value
101
digest = fields.getfirst("digest").value
102
text = fields.getfirst("text").value
103
except AttributeError:
104
# Any of the getfirsts returned None
105
req.throw_error(req.HTTP_BAD_REQUEST)
107
# Open an HTTP connection
108
url = ("http://" + urllib.quote(host) + ":" + urllib.quote(port)
110
body = ("digest=" + urllib.quote(digest)
111
+ "&text=" + urllib.quote(text) + '\n\n')
112
headers = {"Content-Type": "application/x-www-form-urlencoded"}
114
conn = httplib.HTTPConnection(host, port)
115
conn.request("POST", url, body, headers)
117
response = conn.getresponse()
119
req.status = response.status
120
# NOTE: Ignoring arbitrary headers returned by the server
121
# Probably not necessary to proxy them
122
req.content_type = response.getheader("Content-Type", "text/plain")
123
req.write(response.read())
126
req.throw_error(req.HTTP_BAD_REQUEST)