Handling
Files Through Remote Server In Python
Many times Python developers
want to handle files like Read. Write, Create and Update operations on files
through a Remote Server on PC by Python Programming Language. Basically it is a
challenge to the developers to work on Remote server and change the files but
main problem is how we could do that, we have to understand the concept.
So,
let’s start our tutorial for this we are
going to use Robot
Framework Remote Server.But first we have to understand the concept of Robot
Framework...
Robot
Framework
Robot Framework remote
servers allow hosting test libraries on different processes or machines than
Robot Framework itself is running on. This project implements a generic remote
server using the Python programming language. See the remote library interface
documentation for more information about the remote interface in general as
well as for a list of remote server implementations in other programming
languages.
Installation
To install the Robot
Framework we have to use pip :-
$ pip install robotremoteserver
Alternatively you can
download the source distribution from PyPI, extract it and install the remote
server using:
$ python setup.py install
Starting
remote server
The remote server can be
started simply by creating an instance of the server and passing a test library
instance or module to it, given below :-
from robotremoteserver
import RobotRemoteServer
from mylibrary import
MyLibrary
RobotRemoteServer(MyLibrary())
By default the server
listens to address 127.0.0.1 and port 8270. As discussed above, the remote
server accepts various configuration parameters. Some of them are used by this
example which is given below :-
from robotremoteserver
import RobotRemoteServer
from examplelibrary import
ExampleLibrary
RobotRemoteServer(ExampleLibrary(),
host='10.0.0.42', port=0,
port_file='/tmp/remote-port.txt')
Starting from version 0.1,
the server can be initialized without starting it by using the argument
serve=False. The server can then started afterwards by calling its serve method
explicitly. This example is functionally equivalent to the example above:
from robotremoteserver
import RobotRemoteServer
from examplelibrary import
ExampleLibrary
server = RobotRemoteServer(ExampleLibrary(),
host='10.0.0.42', port=0,
port_file='/tmp/remote-port.txt',
serve=False)
server.serve()
Starting
server by threading
The main benefit of
separately initializing and starting the server is that it makes it easier to
start the server in a background thread. Servers started in a thread work
exactly like servers running in the main tread except that stopping the server
forcefully using Ctrl-C or signals is not supported automatically. Users must
thus register signal handlers separately if needed.
import signal
import threading
from examplelibrary import
ExampleLibrary
from robotremoteserver
import RobotRemoteServer
server =
RobotRemoteServer(ExampleLibrary(), port=0, serve=False)
signal.signal(signal.SIGINT,
lambda signum, frame: server.stop())
server_thread =
threading.Thread(target=server.serve)
server_thread.start()
while
server_thread.is_alive():
server_thread.join(0.1)
Stopping
remote server
The
remote server could be forcefully stopped using several different methods that
we are going to use on the program :-
·
Hitting Ctrl-C
on the console where the server is running. Not supported automatically if the
server is started on a background thread.
·
Sending the
process SIGINT, SIGTERM, or SIGHUP signal. Does not work on Windows and not
supported if the server is started on a background thread.
·
Using Stop
Remote Server keyword. Can be disabled by using allow_remote_stop=False when
initializing the server.
·
Using
stop_remote_server function in the XML-RPC interface. Can be disabled with the
allow_remote_stop=False initialization parameter.
·
Running python
-m robotremoteserver stop [uri] which uses the aforementioned
stop_remote_server XML-RPC function internally. Can be disabled with the
allow_remote_stop=False initialization parameter.
·
Using the
stop_remote_server function provided by the robotremoteserver module similarly
as when testing is server running. Uses the stop_remote_server XML-RPC function
internally and can be disabled with the allow_remote_stop=False initialization
parameter.
·
Calling the
stop method of the running server instance. Mainly useful when running the
server on background.
Test Is
server running ?
Starting from the version
0.1, the robotremoteserver module supports for test your framework is running
or not. This can be accomplished by running the module as a script with test
argument and an optional URI:-
$ python -m
robotremoteserver test
Remote server running at
http://127.0.0.1:8270.
$ python -m
robotremoteserver test http://10.0.0.42:57347
No remote server running
at http://10.0.0.42:57347.
Starting from the version
0.1, the robotremoteserver module contains function test_remote_server that can
be used programmatically:-
from robotremoteserver
import test_remote_server
if test_remote_server('http://localhost:8270'):
print('Remote server
running!')
The robotremoteserver module
can be also used to stop a remote server by using stop argument on the command
line or by using the stop_server function programmatically. Testing and
stopping should work also with other Robot Framework remote server
implementations.
Program
to handle file remotly
from __future__ import
print_function
import inspect
import os
import re
import select
import signal
import sys
import threading
import traceback
if sys.version_info <
(3,):
from SimpleXMLRPCServer
import SimpleXMLRPCServer
from StringIO import
StringIO
from xmlrpclib import
Binary, ServerProxy
from collections import
Mapping
PY2, PY3 = True, False
else:
from io import StringIO
from xmlrpc.client import Binary,
ServerProxy
from xmlrpc.server import
SimpleXMLRPCServer
from collections.abc
import Mapping
PY2, PY3 = False, True
unicode = str
long = int
__all__ = ['Server',
'stop_server']
__version__ = '0.1'
BINARY = re.compile('[\x00-\x08\x0B\x0C\x0E-\x1F]')
NON_ASCII =
re.compile('[\x80-\xff]')
class Server(object):
def main() :
remote_file_name =
"home/value.txt"
local_file_name =
"home/ubuntu/desktop/new.txt_from-remote"
cmd = 'python "' +
squishinfo.testCase + '/../py_client.py"'
+ 'get "' +
remote_file_name + '""' + local_file_name + '"'
result = OS.system(cmd)
if (result != 0) :
test.log("An error
occurred")
local_file_name =
"home/ubuntu/desktop/new.txt"
remote_file_name =
"home/ubuntu/value.txt”
cmd = 'python "' + squishinfo.testCase
+ '/../py_remote_file_client.py"'
+ 'get "' +
remote_file_name + '""' + local_file_name + '"'
result = OS.system(cmd)
if (result != 0) :
print("An error
occurred")
def stop_server(uri,
log=True):
logger = print if log else
lambda message: None
if not
test_remote_server(uri, log=False):
logger('No remote server
running at %s.' % uri)
return True
logger('Stopping remote
server at %s.' % uri)
if not
ServerProxy(uri).stop_remote_server():
logger('Stopping not
allowed!')
return False
return True
if __name__ == '__main__':
def parsing(script,
*args):
actions = {'stop':
stop_server}
if not (0 < len(args)
< 3) or args[0] not in actions:
sys.exit('Usage: %s {test|stop} [uri]' %
os.path.basename(script))
uri = args[1] if len(args)
== 2 else 'http://127.0.0.1:8270'
if '://' not in uri:
uri = 'http://' + uri
return actions[args[0]],
uri
action, uri =
parse_args(*sys.argv)
success = action(uri)
sys.exit(0 if success else
1)
Leave a Comment below friends If you guys have some questions to ask,we will give your answers soon....
If you Guys like my tutorial plz share it with your friends↝↜ Thank you Friends and Good Bye...
If you Guys like my tutorial plz share it with your friends↝↜ Thank you Friends and Good Bye...
>>> Compare two voices by python >>> Pandas Tutorial
0 Comments