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.
 
 
 
 
 
 

145 regels
4.5 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. import webnotes
  23. import webnotes.utils
  24. class FrameworkServer:
  25. """
  26. Connect to a remote server via HTTP (webservice).
  27. * `remote_host` is the the address of the remote server
  28. * `path` is the path of the Framework (excluding index.cgi)
  29. """
  30. def __init__(self, remote_host, path, user='', password='', account='', cookies=None, opts=None, https = 0):
  31. # validate
  32. if not (remote_host and path):
  33. raise Exception, "Server address and path necessary"
  34. if not ((user and password) or (cookies)):
  35. raise Exception, "Either cookies or user/password necessary"
  36. self.remote_host = remote_host
  37. self.path = path
  38. self.cookies = cookies or {}
  39. self.webservice_method='POST'
  40. self.account = account
  41. self.account_id = None
  42. self.https = https
  43. self.conn = None
  44. # login
  45. if not cookies:
  46. args = { 'usr': user, 'pwd': password, 'ac_name': account }
  47. if opts:
  48. args.update(opts)
  49. res = self.http_get_response('login', args)
  50. ret = res.read()
  51. try:
  52. ret = eval(ret)
  53. except Exception, e:
  54. webnotes.msgprint(ret)
  55. raise Exception, e
  56. if 'message' in ret and ret['message']!='Logged In':
  57. webnotes.msgprint(ret.get('server_messages'), raise_exception=1)
  58. if ret.get('exc'):
  59. raise Exception, ret.get('exc')
  60. self._extract_cookies(res)
  61. self.account_id = self.cookies.get('account_id')
  62. self.sid = self.cookies.get('sid')
  63. self.login_response = res
  64. self.login_return = ret
  65. # -----------------------------------------------------------------------------------------
  66. def http_get_response(self, method, args):
  67. """
  68. Run a method on the remote server, with the given arguments
  69. """
  70. # get response from remote server
  71. import urllib, urllib2, os
  72. args['cmd'] = method
  73. if self.path.startswith('/'): self.path = self.path[1:]
  74. protocol = self.https and 'https://' or 'http://'
  75. req = urllib2.Request(protocol + os.path.join(self.remote_host, self.path, 'index.cgi'), \
  76. urllib.urlencode(args))
  77. for key in self.cookies:
  78. req.add_header('cookie', '; '.join(['%s=%s' % (key, self.cookies[key]) \
  79. for key in self.cookies]))
  80. return urllib2.urlopen(req)
  81. # -----------------------------------------------------------------------------------------
  82. def _extract_cookies(self, res):
  83. import Cookie
  84. cookies = Cookie.SimpleCookie()
  85. cookies.load(res.headers.get('set-cookie'))
  86. for c in cookies.values():
  87. self.cookies[c.key] = c.value.rstrip(',')
  88. # -----------------------------------------------------------------------------------------
  89. def runserverobj(self, doctype, docname, method, arg=''):
  90. """
  91. Returns the response of a remote method called on a system object specified by `doctype` and `docname`
  92. """
  93. import json
  94. res = self.http_get_response('runserverobj', args = {
  95. 'doctype':doctype
  96. ,'docname':docname
  97. ,'method':method
  98. ,'arg':arg
  99. })
  100. ret = json.loads(res.read())
  101. if ret.get('exc'):
  102. raise Exception, ret.get('exc')
  103. return ret
  104. # -----------------------------------------------------------------------------------------
  105. def run_method(self, method, args={}):
  106. """
  107. Run a method on the remote server
  108. """
  109. res = self.http_get_response(method, args).read()
  110. import json
  111. try:
  112. ret = json.loads(res)
  113. except Exception, e:
  114. webnotes.msgprint('Bad Response: ' + res, raise_exception=1)
  115. if ret.get('exc'):
  116. raise Exception, ret.get('exc')
  117. return ret