httpclient Module
Gaffer provides you a simple Client to control a gaffer node via HTTP.
Example of usage:
import pyuv
from gaffer.httpclient import Server
# initialize a loop
loop = pyuv.Loop.default_loop()
s = Server("http://localhost:5000", loop=loop)
# add a process without starting it
process = s.add_process("dummy", "/some/path/to/dummy/script", start=False)
# start a process
process.start()
# increase the number of process by 2 (so 3 will run)
process.add(2)
# stop all processes
process.stop()
loop.run()
-
class gaffer.httpclient.EventsourceClient(loop, url, **kwargs)[source]
Bases: object
simple client to fetch Gaffer streams using the eventsource
stream.
Example of usage:
loop = pyuv.Loop.default_loop()
def cb(event, data):
print(data)
# create a client
url = http://localhost:5000/streams/1/stderr?feed=continuous'
client = EventSourceClient(loop, url)
# subscribe to the stderr event
client.subscribe("stderr", cb)
# start the client
client.start()
-
render(event, data)[source]
-
run()[source]
-
start()[source]
-
stop()[source]
-
subscribe(event, listener)[source]
-
subscribe_once(event, listener)[source]
-
unsubscribe(event, listener)[source]
-
exception gaffer.httpclient.GafferConflict[source]
Bases: exceptions.Exception
exption raised on HTTP 409
-
exception gaffer.httpclient.GafferNotFound[source]
Bases: exceptions.Exception
exception raised on HTTP 404
-
class gaffer.httpclient.HTTPClient(async_client_class=None, loop=None, **kwargs)[source]
Bases: object
A blocking HTTP client.
This interface is provided for convenience and testing; most applications
that are running an IOLoop will want to use AsyncHTTPClient instead.
Typical usage looks like this:
http_client = httpclient.HTTPClient()
try:
response = http_client.fetch("http://www.friendpaste.com/")
print response.body
except httpclient.HTTPError as e:
print("Error: %s" % e)
-
close()[source]
Closes the HTTPClient, freeing any resources used.
-
fetch(request, **kwargs)[source]
Executes a request, returning an HTTPResponse.
The request may be either a string URL or an HTTPRequest object.
If it is a string, we construct an HTTPRequest using any additional
kwargs: HTTPRequest(request, **kwargs)
If an error occurs during the fetch, we raise an HTTPError.
-
class gaffer.httpclient.Process(server, process)[source]
Bases: object
Process object. Represent a remote process state
-
active[source]
return True if the process is active
-
add(num=1)[source]
increase the number of processes for this template
-
info()[source]
return the process info dict
-
numprocesses[source]
return the maximum number of processes that can be launched
for this template
-
pids[source]
return a list of running pids
-
restart()[source]
restart the process
-
running[source]
return the number of processes running for this template
-
signal(num_or_str)[source]
send a signal to all processes of this template
-
start()[source]
start the process if not started, spawn new processes
-
stats()[source]
-
status()[source]
Return the status
{
"active": true,
"running": 1,
"numprocesses": 1
}
-
stop()[source]
stop the process
-
sub(num=1)[source]
decrease the number of processes for this template
-
class gaffer.httpclient.ProcessId(server, pid, process)[source]
Bases: object
Process Id object. It represent a pid
-
active[source]
return True if the process is active
-
signal(num_or_str)[source]
Send a signal to the pid
-
stop()[source]
stop the process
-
class gaffer.httpclient.Server(uri, loop=None, **options)[source]
Bases: object
Server, main object to connect to a gaffer node. Most of the
calls are blocking. (but running in the loop)
-
add_process(name, cmd, **kwargs)[source]
add a process. Use the same arguments as in save_process.
If a process with the same name is already registred a
GafferConflict exception is raised.
-
get_group(name)[source]
return the list of all process templates of this group
-
get_process(name_or_id)[source]
get a process by name or id.
If id is given a ProcessId instance is returned in other cases a
Process instance is returned.
-
get_watcher(heartbeat='true')[source]
return a watcher to listen on /watch
-
groups()[source]
return the list of all groups
-
is_process(name)[source]
is the process exists ?
-
json_body(resp)[source]
-
processes()[source]
get list of registered processes
-
remove_group(name)[source]
remove the group and all process templates of the group
-
remove_process(name)[source]
Stop a process and remove it from the managed processes
-
request(method, path, headers=None, body=None, **params)[source]
-
restart_group(name)[source]
restart all process templates of the group
-
running()[source]
get list of running processes by pid
-
save_process(name, cmd, **kwargs)[source]
save a process.
Args:
- name: name of the process
- cmd: program command, string)
- args: the arguments for the command to run. Can be a list or
a string. If args is a string, it’s splitted using
shlex.split(). Defaults to None.
- env: a mapping containing the environment variables the command
will run with. Optional
- uid: int or str, user id
- gid: int or st, user group id,
- cwd: working dir
- detach: the process is launched but won’t be monitored and
won’t exit when the manager is stopped.
- shell: boolean, run the script in a shell. (UNIX
only),
- os_env: boolean, pass the os environment to the program
- numprocesses: int the number of OS processes to launch for
this description
If _force_update=True is passed, the existing process template
will be overwritten.
-
send_signal(name_or_id, num_or_str)[source]
Send a signal to the pid or the process name
-
start_group(name)[source]
start all process templates of the group
-
stop_group(name)[source]
stop all process templates of the group
-
update_process(name, cmd, **kwargs)[source]
update a process.
-
version[source]
get gaffer version
-
class gaffer.httpclient.Watcher(loop, url, **kwargs)[source]
Bases: gaffer.httpclient.EventsourceClient
simple EventsourceClient wrapper that decode the JSON to a
python object
-
render(event, data)[source]
-
gaffer.httpclient.encode(v, charset='utf8')[source]
-
gaffer.httpclient.make_uri(base, *args, **kwargs)[source]
Assemble a uri based on a base, any number of path segments,
and query string parameters.
-
gaffer.httpclient.url_encode(obj, charset='utf8', encode_keys=False)[source]
-
gaffer.httpclient.url_quote(s, charset='utf-8', safe='/:')[source]
URL encode a single string with a given encoding.