File: server.rst

package info (click to toggle)
rpy2 3.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,412 kB
  • sloc: python: 18,448; ansic: 492; makefile: 197; sh: 166
file content (62 lines) | stat: -rw-r--r-- 1,598 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
.. _misc-server:

*************
Client-Server
*************

Rserve is currently the default solution when looking
for a server solution for R, but :mod:`rpy2` can be used
to develop very easily one's own server, tailored to answer
specific requirements. Such requirements can include for example
access restriction, a security model, access to subsets of the R
engine, distribution of jobs to other servers, all of which
are currently difficult or impossible to achieve with R serve.


The :mod:`pyRserve` package addresses the connection to Rserve
from Python, and although it frees one from handling the R server is
also constrains one to use Rserve.


Simple socket-based server and client
=====================================

Server
------

An implementation of a simplistic socket server listening
on a given port for a string to evaluate as R code
is straightforward with Python's SocketServer module.

Our example server will be in a file `rpyserve.py`, containing
the following code.

.. literalinclude:: _static/demos/rpyserve.py


Running a server listening on port 9090 is then:

.. code-block:: bash

   python rpyserve.py --hostname localhost


Client
------

Using Python's socket module, implementing a client is
just as easy. We write the code for ours into a file
`rpyclient.py`:
 
.. literalinclude:: _static/demos/rpyclient.py


Evaluating R code on a local server as defined in the previous
section, listening on port 9090 is then:

.. code-block:: bash

   echo 'R.version' | python rpyclient.py --hostname localhost

In this example, the client is querying the R version.