Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

120 rader
3.3 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, 'acx': 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'), urllib.urlencode(args))
  55. for key in self.cookies:
  56. req.add_header('cookie', '; '.join(['%s=%s' % (key, self.cookies[key]) for key in self.cookies]))
  57. return urllib2.urlopen(req)
  58. # -----------------------------------------------------------------------------------------
  59. def _extract_cookies(self, res):
  60. import Cookie
  61. cookies = Cookie.SimpleCookie()
  62. cookies.load(res.headers.get('set-cookie'))
  63. for c in cookies.values():
  64. self.cookies[c.key] = c.value.rstrip(',')
  65. # -----------------------------------------------------------------------------------------
  66. def runserverobj(self, doctype, docname, method, arg=''):
  67. """
  68. Returns the response of a remote method called on a system object specified by `doctype` and `docname`
  69. """
  70. import json
  71. res = self.http_get_response('runserverobj', args = {
  72. 'doctype':doctype
  73. ,'docname':docname
  74. ,'method':method
  75. ,'arg':arg
  76. })
  77. ret = json.loads(res.read())
  78. if ret.get('exc'):
  79. raise Exception, ret.get('exc')
  80. return ret
  81. # -----------------------------------------------------------------------------------------
  82. def run_method(self, method, args={}):
  83. """
  84. Run a method on the remote server
  85. """
  86. res = self.http_get_response(method, args).read()
  87. import json
  88. try:
  89. ret = json.loads(res)
  90. except Exception, e:
  91. webnotes.msgprint('Bad Response: ' + res, raise_exception=1)
  92. if ret.get('exc'):
  93. raise Exception, ret.get('exc')
  94. return ret