def test_close_session(self): client = ServerProxy('http://localhost/test_xmlrpc_ok', loop=self.loop) response = self.loop.run_until_complete( client.name.space.proxfyiedcall()) self.assertEqual(response, 1) self.assertIs(self.loop, client._loop) self.loop.run_until_complete(client.close())
def test_close_session(self): client = ServerProxy('http://localhost/test_xmlrpc_ok', loop=self.loop) response = self.loop.run_until_complete( client.name.space.proxfyiedcall() ) self.assertEqual(response, 1) self.assertIs(self.loop, client._loop) self.loop.run_until_complete(client.close())
def test_close_transport(self): from aioxmlrpc.client import ServerProxy, AioTransport transp = AioTransport(use_https=False, loop=self.loop) transp._connector.close = mock.Mock() client = ServerProxy('http://localhost/test_xmlrpc_ok', loop=self.loop, transport=transp) response = self.loop.run_until_complete( client.name.space.proxfyiedcall()) client.close() self.assertEqual(response, 1) self.assertIs(self.loop, client._loop) self.assertTrue(transp._connector.close.called)
def test_http_500(self): client = ServerProxy('http://localhost/test_http_500', loop=self.loop, session=self.session) with self.assertRaises(ProtocolError): self.loop.run_until_complete(client.name.space.proxfyiedcall())
def test_xmlrpc_fault(self): client = ServerProxy('http://localhost/test_xmlrpc_fault', loop=self.loop, session=self.session) with self.assertRaises(Fault): self.loop.run_until_complete(client.name.space.proxfyiedcall())
def test_xmlrpc_ok(self): from aioxmlrpc.client import ServerProxy client = ServerProxy('http://localhost/test_xmlrpc_ok', loop=self.loop) response = self.loop.run_until_complete( client.name.space.proxfyiedcall()) self.assertEqual(response, 1) self.assertIs(self.loop, client._loop)
async def remote_estimate(n): print(f"Requesting that slave estimate pi with {n} throws.") # Create the proxy in a nice way so it gets closed when we are done. async with ServerProxy('http://localhost:9000') as proxy: pi_remote = await proxy.estimate_pi(n) print(f"Result of remote estimation: pi={pi_remote:.010f}") return pi_remote
def from_opensubtitles(media_path, requested_language=None): searches = [] hash, size = hash_opensubtitles(media_path) if hash is not None: server = ServerProxy('http://api.opensubtitles.org/xml-rpc') log.debug("Authenticating to opensubtitles") response = yield from server.LogIn('', '', 'eng', 'aesop v0.1') token = response['token'] searches.append({'moviehash': hash, 'moviebytesize': str(size)}) log.debug("Searching opensubtitles for {}", searches) response = yield from server.SearchSubtitles(token, searches) raw_subtitles = response['data'] if not raw_subtitles: raw_subtitles = [] subtitles = [] for subtitle in raw_subtitles: lang = subtitle['SubLanguageID'] url = subtitle['SubDownloadLink'] downloads = subtitle['SubDownloadsCnt'] format = subtitle['SubFormat'] if format != 'srt': continue if requested_language is not None and lang != requested_language: continue sub = AvailableSubtitle(downloads, lang, url) subtitles.append(sub) yield from server.LogOut(token) yield from server.close() else: subtitles = [] log.info("{} subtitles for {}", len(subtitles), media_path) return subtitles
def test_xmlrpc_ok_global_loop(self): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) from aioxmlrpc.client import ServerProxy client = ServerProxy('http://localhost/test_xmlrpc_ok') response = self.loop.run_until_complete( client.name.space.proxfyiedcall()) self.assertIs(loop, client._loop) self.assertEqual(response, 1)
def routine(future, endpoint, apikey): api = ServerProxy(endpoint, allow_none=True) result = yield from api.version.info(apikey) future.set_result(result)
def test_http_500(self): from aioxmlrpc.client import ServerProxy, ProtocolError client = ServerProxy('http://localhost/test_http_500', loop=self.loop) self.assertRaises(ProtocolError, self.loop.run_until_complete, client.name.space.proxfyiedcall())
def test_xmlrpc_fault(self): from aioxmlrpc.client import ServerProxy, Fault client = ServerProxy('http://localhost/test_xmlrpc_fault', loop=self.loop) self.assertRaises(Fault, self.loop.run_until_complete, client.name.space.proxfyiedcall())
def main(): client = ServerProxy(ENDPOINT, allow_none=True) for _ in range(5): data = yield from client.version.info() print(data) yield from client.close()
def test_http_error(self): client = ServerProxy('http://nonexistent/nonexistent', loop=self.loop, session=self.session) with self.assertRaises(ProtocolError): self.loop.run_until_complete(client.name.space.proxfyiedcall())
def run_server(hostname, port, htpasswd, token_secret, postgres_uri, trac_xmlrpc_uri, *, svn_uri, svn_username=None, svn_password=None, worker_ssh_params, enable_cors=False, skip_svn_sync=False, skip_trac_sync=False, skip_checking=False, skip_reporting=False): shutdown_timeout = 10 credentials_checker = HtpasswdCredentialsChecker(htpasswd) token_provider = JWTTokenProvider(token_secret) loop = asyncio.get_event_loop() if False: # TODO: asyncssh currently broken due to this asyncio.set_event_loop(None) # See <https://github.com/python/asyncio/issues/478#issuecomment-268476438> # for details. asyncio.get_child_watcher().attach_loop(loop) _setup_termination(loop=loop) _setup_sentry(loop=loop) with contextlib.ExitStack() as exit_stack: exit_stack.callback(loop.close) db = Database(postgres_uri, loop=loop) loop.run_until_complete(db.start()) exit_stack.callback( lambda: loop.run_until_complete(db.stop())) trac_rpc = ServerProxy(trac_xmlrpc_uri, loop=loop) exit_stack.callback(trac_rpc.close) async def do_tickets_sync(): await sync_tickets( db, trac_rpc, COMPONENT_TO_ASSIGNMENT_ID) async def do_svn_sync(): await sync_svn( db, PATH_TO_ASSIGNMENT_ID, svn_uri=svn_uri, svn_username=svn_username, svn_password=svn_password, loop=loop) async def do_check_solutions(): await check_solutions(db, LINKED_PTR_ASSIGNMENT_ID, ssh_params=worker_ssh_params, loop=loop) await check_solutions(db, LAZY_STRING_ASSIGNMENT_ID, ssh_params=worker_ssh_params, loop=loop) await check_solutions(db, FUNCTION_ASSIGNMENT_ID, ssh_params=worker_ssh_params, loop=loop) await check_solutions(db, BIND_ASSIGNMENT_ID, ssh_params=worker_ssh_params, loop=loop) async def do_post_reports(): await report_solutions( db, trac_rpc, LINKED_PTR_ASSIGNMENT_ID, loop=loop) await report_solutions( db, trac_rpc, LAZY_STRING_ASSIGNMENT_ID, loop=loop) await report_solutions( db, trac_rpc, FUNCTION_ASSIGNMENT_ID, loop=loop) await report_solutions( db, trac_rpc, BIND_ASSIGNMENT_ID, loop=loop) # if False: # loop.run_until_complete( # db.get_checkable_solutions(ASSIGNMENT_ID)) # return # if False: # loop.run_until_complete( # check_solutions(db, ASSIGNMENT_ID, # ssh_params=worker_ssh_params, loop=loop)) # return # if False: # loop.run_until_complete( # report_solutions(db, trac_rpc, ASSIGNMENT_ID, # loop=loop)) # return if _DEBUG_SYNC_TICKETS: loop.run_until_complete(do_tickets_sync()) if _DEBUG_SYNC_SVN: loop.run_until_complete(do_svn_sync()) if not skip_svn_sync: svn_sync = PeriodicScheduler( do_svn_sync, 30, "svn_sync", timeout=60 * 10, loop=loop) loop.run_until_complete(svn_sync.start()) exit_stack.callback(lambda: loop.run_until_complete(svn_sync.stop())) if not skip_trac_sync: trac_sync = PeriodicScheduler( do_tickets_sync, 600, "trac_sync", timeout=60 * 10, loop=loop) loop.run_until_complete(trac_sync.start()) exit_stack.callback(lambda: loop.run_until_complete(trac_sync.stop())) if not skip_checking: check_solutions_sync = PeriodicScheduler( do_check_solutions, 30, "check_solutions_sync", timeout=60 * 10, loop=loop) loop.run_until_complete(check_solutions_sync.start()) exit_stack.callback(lambda: loop.run_until_complete(check_solutions_sync.stop())) if not skip_reporting: post_reports = PeriodicScheduler( do_post_reports, 30, "post_reports", timeout=60 * 10, loop=loop) loop.run_until_complete(post_reports.start()) exit_stack.callback(lambda: loop.run_until_complete(post_reports.stop())) app = aiohttp.web.Application(loop=loop) # Start application server. app_server = Server( app, credentials_checker, token_provider, db, loop=loop, enable_cors=enable_cors) loop.run_until_complete(app_server.start()) handler = app.make_handler() socket_server = loop.run_until_complete( loop.create_server(handler, hostname, port)) def stop_app(): _logger.info("Stopping web application...") socket_server.close() loop.run_until_complete(socket_server.wait_closed()) loop.run_until_complete(app.shutdown()) loop.run_until_complete( handler.finish_connections(shutdown_timeout)) loop.run_until_complete(app.cleanup()) # Stop our server first to allow graceful termination of persistent # connections (e.g. WebSockets). # Notice that top of the stack will be executed earlier. exit_stack.callback(stop_app) exit_stack.callback( lambda: loop.run_until_complete(app_server.stop())) url = yarl.URL('http://example.org').\ with_host(hostname).with_port(port) _logger.info("Server started on {}".format(url.human_repr())) loop.run_forever() return 0
class Requester: def __init__(self, credentials: CredentialsBase): self.cred = credentials self.common = AioServerProxy( f'https://{self.cred.domain}/xmlrpc/2/common') self.models = AioServerProxy( f'https://{self.cred.domain}/xmlrpc/2/object') async def execute_kw(self, *args, **kwargs): try: return await self.models.execute_kw(self.cred.db, self.cred.uid, self.cred.dbpass, *args, kwargs) except xmlrpc.client.Fault as exc: if exc.faultCode == 3: print('Credentials ot working. trying re-login') else: raise await self.login(force=True) return self.models.execute_kw(self.cred.db, self.cred.uid, self.cred.dbpass, *args, kwargs) async def login(self, force=False): if self.cred.uid is not None and not force: return uid = await self.common.authenticate(self.cred.db, self.cred.mno, self.cred.dbpass, {}) if uid is not False: self.cred.uid = uid return try: self.cred.get_or_set( 'dbpass', 'login failed for {user}@{db}. Reenter password', passw=True, force=True) except KeyboardInterrupt: try: i = int( input(""" options: [1] change user [2] change db [3] exit choice: """)) except ValueError as exc: raise ValueError('Only integers allowed...') from exc if i == 1: self.cred.get_or_set('mno', 'Enter new MemberNumber for {db}: ', force=True) elif i == 3: sys.exit() else: raise NotImplementedError() self.cred.clear('dbpass') await self.login() async def __aenter__(self): await self.login() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() async def __aexit__(self, *args): return self.__exit__(*args) def close(self): self.common.close() self.models.close()
def __init__(self, credentials: CredentialsBase): self.cred = credentials self.common = AioServerProxy( f'https://{self.cred.domain}/xmlrpc/2/common') self.models = AioServerProxy( f'https://{self.cred.domain}/xmlrpc/2/object')