~registry/weblive/ltsp-cluster-agent-weblive

« back to all changes in this revision

Viewing changes to plugins/jsonlink/__init__.py

  • Committer: Stéphane Graber
  • Date: 2012-08-07 14:55:31 UTC
  • Revision ID: stgraber@ubuntu.com-20120807145531-az664vmfj126z0yv
PEP8 on everything else

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import json
 
2
import socket
 
3
import threading
 
4
import urlparse
 
5
 
1
6
from LTSPAgent.plugin import Plugin
2
7
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
3
8
from SocketServer import ThreadingMixIn
4
 
import threading, json, urlparse, socket
 
9
 
5
10
 
6
11
class jsonHandler(BaseHTTPRequestHandler):
7
12
    def log_message(self, format, *args):
8
13
        return
9
14
 
10
15
    def do_POST(self):
11
 
        request=self.path.split('/')
 
16
        request = self.path.split('/')
12
17
 
13
18
        if len(request) != 3 or request[2] != "json":
14
19
            self.send_response(404)
15
20
            self.end_headers()
16
21
            return
17
22
 
18
 
        plugin=request[1]
 
23
        plugin = request[1]
19
24
        if not plugin in self.server.plugins:
20
25
            self.send_response(404)
21
26
            self.end_headers()
22
27
            return
23
28
 
24
 
        if not hasattr(self.server.plugins[plugin],'json_handler'):
 
29
        if not hasattr(self.server.plugins[plugin], 'json_handler'):
25
30
            self.send_response(404)
26
31
            self.end_headers()
27
32
            return
28
33
 
29
34
        try:
30
 
            length=self.headers.getheader('content-length','0')
31
 
            content=self.rfile.read(int(length))
32
 
            post=urlparse.parse_qs(content)
 
35
            length = self.headers.getheader('content-length', '0')
 
36
            content = self.rfile.read(int(length))
 
37
            post = urlparse.parse_qs(content)
33
38
            if "query" in post:
34
 
                query=json.loads(post['query'][0])
 
39
                query = json.loads(post['query'][0])
35
40
            else:
36
41
                self.send_response(404)
37
42
                self.end_headers()
46
51
            self.end_headers()
47
52
            return
48
53
 
49
 
        client=self.headers.getheader('X-Forwarded-For',self.client_address[0])
 
54
        client = self.headers.getheader('X-Forwarded-For',
 
55
                                        self.client_address[0])
50
56
 
51
57
        self.send_response(200)
52
58
        self.end_headers()
53
 
        self.server.LOGGER.debug("Remote call on %s with params %s", self.server.plugins[plugin], str(query))
 
59
        self.server.LOGGER.debug("Remote call on %s with params %s",
 
60
                                    self.server.plugins[plugin], str(query))
54
61
        try:
55
 
            self.wfile.write(json.dumps(getattr(self.server.plugins[plugin],'json_handler')(query,client)))
 
62
            self.wfile.write(json.dumps(getattr(self.server.plugins[plugin],
 
63
                                            'json_handler')(query, client)))
56
64
            self.wfile.write('\n')
57
65
        except:
58
66
            self.server.LOGGER.info("Lost connection with %s" % client)
59
67
 
60
68
        return
61
69
 
 
70
 
62
71
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
63
72
    """Handle requests in a separate thread."""
64
73
    def __init__(self, server_address, SecureXMLRPCRequestHandler):
70
79
 
71
80
        HTTPServer.__init__(self, server_address, SecureXMLRPCRequestHandler)
72
81
 
 
82
 
73
83
class jsonserverThread(threading.Thread):
74
84
    def run(self):
75
85
        server = ThreadedHTTPServer(self.connection, jsonHandler)
76
 
        server.plugins=self.plugins
77
 
        server.LOGGER=self.LOGGER
78
 
        self.LOGGER.info("Serving HTTP/JSON on %s port %s", self.connection[0],self.connection[1])
 
86
        server.plugins = self.plugins
 
87
        server.LOGGER = self.LOGGER
 
88
        self.LOGGER.info("Serving HTTP/JSON on %s port %s",
 
89
                            self.connection[0], self.connection[1])
79
90
        server.serve_forever()
80
91
 
 
92
 
81
93
class jsonlink(Plugin):
82
94
    """Export another plugin's functions over JSON"""
83
95
 
84
96
    def init_plugin(self):
85
 
        jsonserver=jsonserverThread()
86
 
        jsonserver.plugins=self.plugins
87
 
        jsonserver.connection=(self.get_config_path(self.config,'general','bindaddr'), int(self.get_config_path(self.config,'general','bindport')))
88
 
        jsonserver.LOGGER=self.LOGGER
 
97
        jsonserver = jsonserverThread()
 
98
        jsonserver.plugins = self.plugins
 
99
        jsonserver.connection = (self.get_config_path(
 
100
                                    self.config, 'general', 'bindaddr'),
 
101
                                    int(self.get_config_path(
 
102
                                        self.config, 'general', 'bindport')))
 
103
        jsonserver.LOGGER = self.LOGGER
89
104
        jsonserver.start()
90
105
 
91
106
        Plugin.init_plugin(self)