File: client.py

package info (click to toggle)
pyro5 5.15-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,112 kB
  • sloc: python: 14,291; makefile: 163; sh: 66; javascript: 62
file content (71 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
import json
import re
import pprint
from urllib.request import urlopen, Request
from urllib.error import HTTPError


def get_charset(req):
    charset = "utf-8"
    match = re.match(r".* charset=(.+)", req.getheader("Content-Type"))
    if match:
        charset = match.group(1)
    return charset


def pyro_call(object_name, method, callback):
    request = Request("http://127.0.0.1:8080/pyro/{0}/{1}".format(object_name, method),
                      # headers={"x-pyro-options": "oneway", "x-pyro-gateway-key": "secretgatewaykey"}
                      )
    with urlopen(request) as req:
        charset = get_charset(req)
        data = req.read().decode(charset)
    if data:
        callback(json.loads(data))
    else:
        callback(None)


def write_result(result):
    pprint.pprint(result, width=40)

try:
    print("\nLIST--->")
    pyro_call("Pyro.NameServer", "list", write_result)
except HTTPError as x:
    print("Error:", x)
    print("Error response data:", x.read())

try:
    print("\nMETA--->")
    pyro_call("Pyro.NameServer", "$meta", write_result)
except HTTPError as x:
    print("Error:", x)
    print("Error response data:", x.read())

try:
    print("\nLOOKUP--->")
    pyro_call("Pyro.NameServer", "lookup?name=Pyro.NameServer", write_result)
except HTTPError as x:
    print("Error:", x)
    print("Error response data:", x.read())

try:
    print("\nONEWAY_SLOW--->")
    pyro_call("test.echoserver", "oneway_slow", write_result)
except HTTPError as x:
    print("Error:", x)
    print("Error response data:", x.read())

try:
    print("\nSLOW--->")
    pyro_call("test.echoserver", "slow", write_result)
except HTTPError as x:
    print("Error:", x)
    print("Error response data:", x.read())

# Note that there is a nicer way to pass the parameters, you can probably
# grab them from a function's vargs and/or kwargs and convert those to
# a querystring using the appropriate library function.
# Then you can call the method as usual and don't have to worry about adding the querystring
# (or sticking it in a POST request if the params are too large)...