25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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