httpgateway.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. """
  2. HTTP gateway: connects the web browser's world of javascript+http and Pyro.
  3. Creates a stateless HTTP server that essentially is a proxy for the Pyro objects behind it.
  4. It exposes the Pyro objects through a HTTP interface and uses the JSON serializer,
  5. so that you can immediately process the response data in the browser.
  6. You can start this module as a script from the command line, to easily get a
  7. http gateway server running:
  8. :command:`python -m Pyro4.utils.httpgateway`
  9. or simply: :command:`pyro4-httpgateway`
  10. It is also possible to import the 'pyro_app' function and stick that into a WSGI
  11. server of your choice, to have more control.
  12. The javascript code in the web page of the gateway server works with the same-origin
  13. browser policy because it is served by the gateway itself. If you want to access it
  14. from scripts in different sites, you have to work around this or embed the gateway app
  15. in your site. Non-browser clients that access the http api have no problems ofcourse.
  16. See the `http` example for two of such clients (node.js and python).
  17. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  18. """
  19. from __future__ import print_function
  20. import sys
  21. import re
  22. import cgi
  23. import uuid
  24. from wsgiref.simple_server import make_server
  25. import traceback
  26. import Pyro4
  27. import Pyro4.errors
  28. import Pyro4.message
  29. import Pyro4.util
  30. import Pyro4.constants
  31. from Pyro4.util import json # don't import directly, we want to use the JSON_MODULE config item
  32. __all__ = ["pyro_app", "main"]
  33. _nameserver = None
  34. def get_nameserver(hmac=None):
  35. global _nameserver
  36. if not _nameserver:
  37. _nameserver = Pyro4.locateNS(hmac_key=hmac)
  38. try:
  39. _nameserver.ping()
  40. return _nameserver
  41. except Pyro4.errors.ConnectionClosedError:
  42. _nameserver = None
  43. print("Connection with nameserver lost, reconnecting...")
  44. return get_nameserver(hmac)
  45. def invalid_request(start_response):
  46. """Called if invalid http method."""
  47. start_response('405 Method Not Allowed', [('Content-Type', 'text/plain')])
  48. return [b'Error 405: Method Not Allowed']
  49. def not_found(start_response):
  50. """Called if Url not found."""
  51. start_response('404 Not Found', [('Content-Type', 'text/plain')])
  52. return [b'Error 404: Not Found']
  53. def redirect(start_response, target):
  54. """Called to do a redirect"""
  55. start_response('302 Found', [('Location', target)])
  56. return []
  57. index_page_template = """<!DOCTYPE html>
  58. <html>
  59. <head>
  60. <title>Pyro HTTP gateway</title>
  61. <style type="text/css">
  62. body {{ margin: 1em; }}
  63. table, th, td {{border: 1px solid #bbf; padding: 4px;}}
  64. table {{border-collapse: collapse;}}
  65. pre {{border: 1px solid #bbf; padding: 1ex; margin: 1ex; white-space: pre-wrap;}}
  66. #title-logo {{ float: left; margin: 0 1em 0 0; }}
  67. </style>
  68. </head>
  69. <body>
  70. <script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
  71. <script>
  72. "use strict";
  73. function pyro_call(name, method, params) {{
  74. $.ajax({{
  75. url: name+"/"+method,
  76. type: "GET",
  77. data: params,
  78. dataType: "json",
  79. // headers: {{ "X-Pyro-Correlation-Id": "11112222-1111-2222-3333-222244449999" }},
  80. // headers: {{ "X-Pyro-Gateway-Key": "secret-key" }},
  81. // headers: {{ "X-Pyro-Options": "oneway" }},
  82. beforeSend: function(xhr, settings) {{
  83. $("#pyro_call").text(settings.type+" "+settings.url);
  84. }},
  85. error: function(xhr, status, error) {{
  86. var errormessage = "ERROR: "+xhr.status+" "+error+" \\n"+xhr.responseText;
  87. $("#pyro_response").text(errormessage);
  88. }},
  89. success: function(data) {{
  90. $("#pyro_response").text(JSON.stringify(data, null, 4));
  91. }}
  92. }});
  93. }}
  94. </script>
  95. <div id="title-logo"><img src="http://pythonhosted.org/Pyro4/_static/pyro.png"></div>
  96. <div id="title-text">
  97. <h1>Pyro HTTP gateway</h1>
  98. <p>Use http+json to talk to Pyro objects. <a href="https://pythonhosted.org/Pyro4/tipstricks.html#pyro-via-http-and-json">Docs.</a></p>
  99. </div>
  100. <p><em>Note: performance isn't maxed; it is stateless. Does a name lookup and uses a new Pyro proxy for each request.</em></p>
  101. <h2>Currently exposed contents of name server on {hostname}:</h2>
  102. <p>(Limited to 10 entries, exposed name pattern = '{ns_regex}')</p>
  103. {name_server_contents_list}
  104. <p>Name server examples: (these examples are working if you expose the Pyro.NameServer object)</p>
  105. <ul>
  106. <li><a href="Pyro.NameServer/$meta" onclick="pyro_call('Pyro.NameServer','$meta'); return false;">Pyro.NameServer/$meta</a> -- gives meta info of the name server (methods)</li>
  107. <li><a href="Pyro.NameServer/list" onclick="pyro_call('Pyro.NameServer','list'); return false;">Pyro.NameServer/list</a> -- lists the contents of the name server</li>
  108. <li><a href="Pyro.NameServer/list?prefix=test." onclick="pyro_call('Pyro.NameServer','list', {{'prefix':'test.'}}); return false;">Pyro.NameServer/list?prefix=test.</a> -- lists the contents of the name server starting with 'test.'</li>
  109. <li><a href="Pyro.NameServer/lookup?name=Pyro.NameServer" onclick="pyro_call('Pyro.NameServer','lookup', {{'name':'Pyro.NameServer'}}); return false;">Pyro.NameServer/lookup?name=Pyro.NameServer</a> -- perform lookup method of the name server</li>
  110. <li><a href="Pyro.NameServer/lookup?name=test.echoserver" onclick="pyro_call('Pyro.NameServer','lookup', {{'name':'test.echoserver'}}); return false;">Pyro.NameServer/lookup?name=test.echoserver</a> -- perform lookup method of the echo server</li>
  111. </ul>
  112. <p>Echoserver examples: (these examples are working if you expose the test.echoserver object)</p>
  113. <ul>
  114. <li><a href="test.echoserver/error" onclick="pyro_call('test.echoserver','error'); return false;">test.echoserver/error</a> -- perform error call on echoserver</li>
  115. <li><a href="test.echoserver/echo?message=Hi there, browser script!" onclick="pyro_call('test.echoserver','echo', {{'message':'Hi there, browser script!'}}); return false;">test.echoserver/echo?message=Hi there, browser script!</a> -- perform echo call on echoserver</li>
  116. </ul>
  117. <h2>Pyro response data (via Ajax):</h2>
  118. Call: <pre id="pyro_call"> &nbsp; </pre>
  119. Response: <pre id="pyro_response"> &nbsp; </pre>
  120. <p>Pyro version: {pyro_version} &mdash; &copy; Irmen de Jong</p>
  121. </body>
  122. </html>
  123. """
  124. def return_homepage(environ, start_response):
  125. try:
  126. nameserver = get_nameserver(hmac=pyro_app.hmac_key)
  127. except Pyro4.errors.NamingError as x:
  128. print("Name server error:", x)
  129. start_response('500 Internal Server Error', [('Content-Type', 'text/plain')])
  130. return [b"Cannot connect to the Pyro name server. Is it running? Refresh page to retry."]
  131. start_response('200 OK', [('Content-Type', 'text/html')])
  132. nslist = ["<table><tr><th>Name</th><th>methods</th><th>attributes (zero-param methods)</th></tr>"]
  133. names = sorted(list(nameserver.list(regex=pyro_app.ns_regex).keys())[:10])
  134. with Pyro4.batch(nameserver) as nsbatch:
  135. for name in names:
  136. nsbatch.lookup(name)
  137. for name, uri in zip(names, nsbatch()):
  138. attributes = "-"
  139. try:
  140. with Pyro4.Proxy(uri) as proxy:
  141. proxy._pyroHmacKey = pyro_app.hmac_key
  142. proxy._pyroBind()
  143. methods = " &nbsp; ".join(proxy._pyroMethods) or "-"
  144. attributes = ["<a href=\"{name}/{attribute}\" onclick=\"pyro_call('{name}','{attribute}'); return false;\">{attribute}</a>"
  145. .format(name=name, attribute=attribute) for attribute in proxy._pyroAttrs]
  146. attributes = " &nbsp; ".join(attributes) or "-"
  147. except Pyro4.errors.PyroError as x:
  148. stderr = environ["wsgi.errors"]
  149. print("ERROR getting metadata for {0}:".format(uri), file=stderr)
  150. traceback.print_exc(file=stderr)
  151. methods = "??error:%s??" % str(x)
  152. nslist.append(
  153. "<tr><td><a href=\"{name}/$meta\" onclick=\"pyro_call('{name}','$meta'); return false;\">{name}</a></td><td>{methods}</td><td>{attributes}</td></tr>"
  154. .format(name=name, methods=methods, attributes=attributes))
  155. nslist.append("</table>")
  156. index_page = index_page_template.format(ns_regex=pyro_app.ns_regex,
  157. name_server_contents_list="".join(nslist),
  158. pyro_version=Pyro4.constants.VERSION,
  159. hostname=environ["SERVER_NAME"])
  160. return [index_page.encode("utf-8")]
  161. def process_pyro_request(environ, path, parameters, start_response):
  162. pyro_options = environ.get("HTTP_X_PYRO_OPTIONS", "").split(",")
  163. if not path:
  164. return return_homepage(environ, start_response)
  165. matches = re.match(r"(.+)/(.+)", path)
  166. if not matches:
  167. return not_found(start_response)
  168. object_name, method = matches.groups()
  169. if pyro_app.gateway_key:
  170. gateway_key = environ.get("HTTP_X_PYRO_GATEWAY_KEY", "") or parameters.get("$key", "")
  171. gateway_key = gateway_key.encode("utf-8")
  172. if gateway_key != pyro_app.gateway_key:
  173. start_response('403 Forbidden', [('Content-Type', 'text/plain')])
  174. return [b"403 Forbidden - incorrect gateway api key"]
  175. if "$key" in parameters:
  176. del parameters["$key"]
  177. if pyro_app.ns_regex and not re.match(pyro_app.ns_regex, object_name):
  178. start_response('403 Forbidden', [('Content-Type', 'text/plain')])
  179. return [b"403 Forbidden - access to the requested object has been denied"]
  180. try:
  181. nameserver = get_nameserver(hmac=pyro_app.hmac_key)
  182. uri = nameserver.lookup(object_name)
  183. with Pyro4.Proxy(uri) as proxy:
  184. header_corr_id = environ.get("HTTP_X_PYRO_CORRELATION_ID", "")
  185. if header_corr_id:
  186. Pyro4.current_context.correlation_id = uuid.UUID(header_corr_id) # use the correlation id from the request header
  187. else:
  188. Pyro4.current_context.correlation_id = uuid.uuid1() # set new correlation id
  189. proxy._pyroHmacKey = pyro_app.hmac_key
  190. proxy._pyroGetMetadata()
  191. if "oneway" in pyro_options:
  192. proxy._pyroOneway.add(method)
  193. if method == "$meta":
  194. result = {"methods": tuple(proxy._pyroMethods), "attributes": tuple(proxy._pyroAttrs)}
  195. reply = json.dumps(result).encode("utf-8")
  196. start_response('200 OK', [('Content-Type', 'application/json; charset=utf-8'),
  197. ('X-Pyro-Correlation-Id', str(Pyro4.current_context.correlation_id))])
  198. return [reply]
  199. else:
  200. proxy._pyroRawWireResponse = True # we want to access the raw response json
  201. if method in proxy._pyroAttrs:
  202. # retrieve the attribute
  203. assert not parameters, "attribute lookup can't have query parameters"
  204. msg = getattr(proxy, method)
  205. else:
  206. # call the remote method
  207. msg = getattr(proxy, method)(**parameters)
  208. if msg is None or "oneway" in pyro_options:
  209. # was a oneway call, no response available
  210. start_response('200 OK', [('Content-Type', 'application/json; charset=utf-8'),
  211. ('X-Pyro-Correlation-Id', str(Pyro4.current_context.correlation_id))])
  212. return []
  213. elif msg.flags & Pyro4.message.FLAGS_EXCEPTION:
  214. # got an exception response so send a 500 status
  215. start_response('500 Internal Server Error', [('Content-Type', 'application/json; charset=utf-8')])
  216. return [msg.data]
  217. else:
  218. # normal response
  219. start_response('200 OK', [('Content-Type', 'application/json; charset=utf-8'),
  220. ('X-Pyro-Correlation-Id', str(Pyro4.current_context.correlation_id))])
  221. return [msg.data]
  222. except Exception as x:
  223. stderr = environ["wsgi.errors"]
  224. print("ERROR handling {0} with params {1}:".format(path, parameters), file=stderr)
  225. traceback.print_exc(file=stderr)
  226. start_response('500 Internal Server Error', [('Content-Type', 'application/json; charset=utf-8')])
  227. reply = json.dumps(Pyro4.util.SerializerBase.class_to_dict(x)).encode("utf-8")
  228. return [reply]
  229. def pyro_app(environ, start_response):
  230. """
  231. The WSGI app function that is used to process the requests.
  232. You can stick this into a wsgi server of your choice, or use the main() method
  233. to use the default wsgiref server.
  234. """
  235. Pyro4.config.SERIALIZER = "json" # we only talk json through the http proxy
  236. Pyro4.config.COMMTIMEOUT = pyro_app.comm_timeout
  237. method = environ.get("REQUEST_METHOD")
  238. path = environ.get('PATH_INFO', '').lstrip('/')
  239. if not path:
  240. return redirect(start_response, "/pyro/")
  241. if path.startswith("pyro/"):
  242. if method in ("GET", "POST"):
  243. parameters = singlyfy_parameters(cgi.parse(environ['wsgi.input'], environ))
  244. return process_pyro_request(environ, path[5:], parameters, start_response)
  245. else:
  246. return invalid_request(start_response)
  247. return not_found(start_response)
  248. def singlyfy_parameters(parameters):
  249. """
  250. Makes a cgi-parsed parameter dictionary into a dict where the values that
  251. are just a list of a single value, are converted to just that single value.
  252. """
  253. for key, value in parameters.items():
  254. if isinstance(value, (list, tuple)) and len(value) == 1:
  255. parameters[key] = value[0]
  256. return parameters
  257. pyro_app.ns_regex = r"http\."
  258. pyro_app.hmac_key = None
  259. pyro_app.gateway_key = None
  260. pyro_app.comm_timeout = Pyro4.config.COMMTIMEOUT
  261. def main(args=None):
  262. from optparse import OptionParser
  263. parser = OptionParser()
  264. parser.add_option("-H", "--host", default="localhost", help="hostname to bind server on (default=%default)")
  265. parser.add_option("-p", "--port", type="int", default=8080, help="port to bind server on (default=%default)")
  266. parser.add_option("-e", "--expose", default=pyro_app.ns_regex, help="a regex of object names to expose (default=%default)")
  267. parser.add_option("-k", "--pyrokey", help="the HMAC key to use to connect with Pyro")
  268. parser.add_option("-g", "--gatewaykey", help="the api key to use to connect to the gateway itself")
  269. parser.add_option("-t", "--timeout", type="float", default=pyro_app.comm_timeout, help="Pyro timeout value to use (COMMTIMEOUT setting, default=%default)")
  270. options, args = parser.parse_args(args)
  271. pyro_app.hmac_key = (options.pyrokey or "").encode("utf-8")
  272. pyro_app.gateway_key = (options.gatewaykey or "").encode("utf-8")
  273. pyro_app.ns_regex = options.expose
  274. pyro_app.comm_timeout = Pyro4.config.COMMTIMEOUT = options.timeout
  275. if pyro_app.ns_regex:
  276. print("Exposing objects with names matching: ", pyro_app.ns_regex)
  277. else:
  278. print("Warning: exposing all objects (no expose regex set)")
  279. try:
  280. ns = get_nameserver(hmac=pyro_app.hmac_key)
  281. except Pyro4.errors.PyroError:
  282. print("Not yet connected to a name server.")
  283. else:
  284. print("Connected to name server at: ", ns._pyroUri)
  285. server = make_server(options.host, options.port, pyro_app)
  286. print("Pyro HTTP gateway running on http://{0}:{1}/pyro/".format(*server.socket.getsockname()))
  287. server.serve_forever()
  288. server.server_close()
  289. return 0
  290. if __name__ == "__main__":
  291. sys.exit(main())