This page gives a good introduction to Archer. It assumes you already have Archer installed. If you do not, head over to the installation
section.
A minimal Archer application looks something like this:
from archer import Archer
app = Archer('PingPong')
@app.api('ping')
def ping():
return 'pong'
provided a hello.thrift file under your working directory, define a service in the file:
service PingPong {
string ping(),
}
Note
Archer
accepts one parameter, that is the name of the service defined in the thrift file.
So what did that code do?
- First we imported the
~archer.Archer
class. An instance of this class will be our Thrift RPC server_side application. - Next we create an instance of this class. The first argument is the name of the application
- We then use the
~archer.Archer.api
decorator to tell Archer what name defined in thrift file should trigger our function. - The function returns the message for
ping
RPC call, which is a stringpong
here - the service PingPong is defined in the hello.thrift file
Just save the python code as hello.py
(or something similar) and the service definition in hello.thrift
, run it with your Python interpreter. Make sure to not call your application archer.py
because this would conflict with Archer itself.
To run the application you can use the archer
command:
$ archer run
* Running on 127.0.0.1:6000 in DEBUG mode
This launches a very simple built_in server, which is good enough for testing but probably not what you want to use in production. For deployment options see deploying
.
Now run call the remote function you can also use the archer
command:
$ archer call ping
* pong
You should see that the string pong is returned
Externally Visible Server
you can make the server publicly available simply by adding --host=0.0.0.0
to the command line:
archer run --host=0.0.0.0
This tells your operating system to listen on all public IPs.