~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/consoleservice/__init__.py

  • Committer: mattgiuca
  • Date: 2008-01-23 05:33:15 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:271
consoleservice: Wrote "chat" mode; works as a proxy to the real console
server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
import os
23
23
import pwd
 
24
import httplib
 
25
import urllib
24
26
 
25
27
import cjson
26
28
 
88
90
    req.write(cjson.encode({"host": host, "port": port, "magic": magic}))
89
91
 
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()
 
98
    try:
 
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)
 
106
 
 
107
    # Open an HTTP connection
 
108
    url = ("http://" + urllib.quote(host) + ":" + urllib.quote(port)
 
109
            + "/chat");
 
110
    body = ("digest=" + urllib.quote(digest)
 
111
            + "&text=" + urllib.quote(text) + '\n\n')
 
112
    headers = {"Content-Type": "application/x-www-form-urlencoded"}
 
113
    try:
 
114
        conn = httplib.HTTPConnection(host, port)
 
115
        conn.request("POST", url, body, headers)
 
116
 
 
117
        response = conn.getresponse()
 
118
        
 
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())
 
124
        conn.close()
 
125
    except:
 
126
        req.throw_error(req.HTTP_BAD_REQUEST)