You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

123 lines
3.4 KiB

  1. import webnotes
  2. import webnotes.utils
  3. class FrameworkServer:
  4. """
  5. Connect to a remote server via HTTP (webservice).
  6. * `remote_host` is the the address of the remote server
  7. * `path` is the path of the Framework (excluding index.cgi)
  8. """
  9. def __init__(self, remote_host, path, user='', password='', account='', cookies=None, opts=None, https = 0):
  10. # validate
  11. if not (remote_host and path):
  12. raise Exception, "Server address and path necessary"
  13. if not ((user and password) or (cookies)):
  14. raise Exception, "Either cookies or user/password necessary"
  15. self.remote_host = remote_host
  16. self.path = path
  17. self.cookies = cookies or {}
  18. self.webservice_method='POST'
  19. self.account = account
  20. self.account_id = None
  21. self.https = https
  22. self.conn = None
  23. # login
  24. if not cookies:
  25. args = { 'usr': user, 'pwd': password, 'ac_name': account }
  26. if opts:
  27. args.update(opts)
  28. res = self.http_get_response('login', args)
  29. ret = res.read()
  30. try:
  31. ret = eval(ret)
  32. except Exception, e:
  33. webnotes.msgprint(ret)
  34. raise Exception, e
  35. if 'message' in ret and ret['message']!='Logged In':
  36. webnotes.msgprint(ret.get('server_messages'), raise_exception=1)
  37. if ret.get('exc'):
  38. raise Exception, ret.get('exc')
  39. self._extract_cookies(res)
  40. self.account_id = self.cookies.get('account_id')
  41. self.sid = self.cookies.get('sid')
  42. self.login_response = res
  43. self.login_return = ret
  44. # -----------------------------------------------------------------------------------------
  45. def http_get_response(self, method, args):
  46. """
  47. Run a method on the remote server, with the given arguments
  48. """
  49. # get response from remote server
  50. import urllib, urllib2, os
  51. args['cmd'] = method
  52. if self.path.startswith('/'): self.path = self.path[1:]
  53. protocol = self.https and 'https://' or 'http://'
  54. req = urllib2.Request(protocol + os.path.join(self.remote_host, self.path, 'index.cgi'), \
  55. urllib.urlencode(args))
  56. for key in self.cookies:
  57. req.add_header('cookie', '; '.join(['%s=%s' % (key, self.cookies[key]) \
  58. for key in self.cookies]))
  59. return urllib2.urlopen(req)
  60. # -----------------------------------------------------------------------------------------
  61. def _extract_cookies(self, res):
  62. import Cookie
  63. cookies = Cookie.SimpleCookie()
  64. cookies.load(res.headers.get('set-cookie'))
  65. for c in cookies.values():
  66. self.cookies[c.key] = c.value.rstrip(',')
  67. # -----------------------------------------------------------------------------------------
  68. def runserverobj(self, doctype, docname, method, arg=''):
  69. """
  70. Returns the response of a remote method called on a system object specified by `doctype` and `docname`
  71. """
  72. import json
  73. res = self.http_get_response('runserverobj', args = {
  74. 'doctype':doctype
  75. ,'docname':docname
  76. ,'method':method
  77. ,'arg':arg
  78. })
  79. ret = json.loads(res.read())
  80. if ret.get('exc'):
  81. raise Exception, ret.get('exc')
  82. return ret
  83. # -----------------------------------------------------------------------------------------
  84. def run_method(self, method, args={}):
  85. """
  86. Run a method on the remote server
  87. """
  88. res = self.http_get_response(method, args).read()
  89. import json
  90. try:
  91. ret = json.loads(res)
  92. except Exception, e:
  93. webnotes.msgprint('Bad Response: ' + res, raise_exception=1)
  94. if ret.get('exc'):
  95. raise Exception, ret.get('exc')
  96. return ret