-
Notifications
You must be signed in to change notification settings - Fork 24
/
tutorial.py
executable file
·117 lines (82 loc) · 3.02 KB
/
tutorial.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
import sys, os, tempfile, random, uuid
# ------------------------------------------
# This is a tutorial introduction to
# pyFreenet, arranged as comments and code
# interspersed in a python script
#
# read through this carefully, and learn
# ------------------------------------------
# ------------------------------------------
# first things first - import fcp module
import fcp
# ------------------------------------------
# state where our FCP port is
fcpHost = "127.0.0.1"
# ------------------------------------------
# create a node connection object
#
# we're setting a relatively high verbosity so you
# can see the traffic
node = fcp.FCPNode(host=fcpHost, verbosity=fcp.DETAIL)
# -----------------------------------------------
# now, perform a simple direct insert of a string
# val = raw_input("Please enter a string to insert: ")
# ksk = raw_input("Please enter a short KSK key name: ")
# val is a binary string since it is being sent as data.
# It can also be assigned via val = "testinsert".encode('utf-8')
val = b"testinsert"
ksk = "testinsertkey" + uuid.uuid4().hex
uri = "KSK@" + ksk
print("Inserting %s, containing '%s'" % (uri, val))
# do the put - note that 'data=' inserts a string directly
# note too that mimetype is optional, defaulting to text/plain
node.put("KSK@"+ksk, data=val, mimetype="text/plain")
print("insert completed successfully")
# ------------------------------------------
# now, retrieve it back
print("trying to retrieve our value back")
mimetype, val1, msg = node.get(uri)
# ensure it's correct
if val == val1:
print("retrieved ok, values match")
else:
print("huh? values don't match")
# ------------------------------------------
# now, insert from a file
# val = raw_input("Please enter a string to insert: ")
# ksk = raw_input("Please enter a short KSK key name: ")
# path = raw_input("Enter a temporary filename: ")
val = "testinsertforfile"
ksk = "testkeyforfile" + uuid.uuid4().hex
tmpdir = tempfile.mkdtemp()
path = os.path.join(tmpdir, "testinsertfile")
# write our string to a file
f = open(path, "w")
f.write(val)
f.close()
uri = "KSK@" + ksk
print("Inserting %s, from file '%s'" % (uri, path))
# do the put - note that 'file=' inserts from a filename or file object
node.put("KSK@"+ksk, file=path)
# ------------------------------------------
# now, demonstrate asynchronous requests
print("Launching asynchronous request")
job = node.get(uri, **{"async": True})
# we can poll the job
if job.isComplete():
print("Yay! job complete")
else:
# or we can await its completion
result = job.wait()
print("Result='%s'" % str(result))
# ------------------------------------------
# similarly, we can get to a file
# path = raw_input("temporary file to retrieve to: ")
path = os.path.join(tmpdir, "testgetfile")
node.get(uri, file=path)
# again, the 'file=' can be a pathname or an open file object
# ------------------------------------------
# TODO: demonstrate persistent requests
# ------------------------------------------
# TODO: demonstrate global requests