Package fcp :: Module node
[hide private]
[frames] | no frames]

Module node

source code

An implementation of a freenet client library for FCP v2, offering considerable flexibility.

Clients should instantiate FCPNode, then execute its methods to perform tasks with FCP.

This module was written by aum, May 2006, released under the GNU Lesser General Public License.

No warranty, yada yada

For FCP documentation, see http://wiki.freenetproject.org/FCPv2

Classes [hide private]
  ConnectionRefused
cannot connect to given host/port
  PrivacyRisk
The following code would pose a privacy risk
  FCPException
  FCPGetFailed
  FCPPutFailed
  FCPProtocolError
  FCPNodeFailure
node seems to have died
  FCPSendTimeout
timed out waiting for command to be sent to node
  FCPNodeTimeout
timed out waiting for node to respond
  FCPNameLookupFailure
name services name lookup failed
  FCPNode
Represents an interface to a freenet node via its FCP port, and exposes primitives for the basic genkey, get, put and putdir operations as well as peer management primitives.
  JobTicket
A JobTicket is an object returned to clients making asynchronous requests.
Functions [hide private]
 
toBool(arg) source code
 
readdir(dirpath, prefix='', gethashes=False)
Reads a directory, returning a sequence of file dicts.
source code
 
hashFile(path)
returns an SHA(1) hash of a file's contents
source code
 
sha256dda(nodehelloid, identifier, path=None)
returns a sha256 hash of a file's contents for bypassing TestDDA
source code
 
guessMimetype(filename)
Returns a guess of a mimetype based on a filename's extension
source code
 
toUrlsafe(filename)
Make a filename url-safe, keeping only the basename and killing all potentially unfitting characters.
source code
 
uriIsPrivate(uri)
analyses an SSK URI, and determines if it is an SSK or USK private key
source code
 
parseTime(t)
Parses a time value, recognising suffices like 'm' for minutes, 's' for seconds, 'h' for hours, 'd' for days, 'w' for weeks, 'M' for months.
source code
 
base64encode(raw)
Encodes a string to base64, using the Freenet alphabet
source code
 
base64decode(enc)
Decodes a freenet-encoded base64 string back to a binary string
source code
 
_base30hex(integer)
Turn an integer into a simple lowercase base30hex encoding.
source code
 
_test() source code
Variables [hide private]
  _pollInterval = 0.03
  defaultFCPHost = '127.0.0.1'
  defaultFCPPort = 9481
  defaultFProxyHost = '127.0.0.1'
  defaultFProxyPort = 8888
  pollTimeout = 0.1
  intKeys = ['DataLength', 'Code']
  expectedVersion = '2.0'
  SILENT = 0
  FATAL = 1
  CRITICAL = 2
  ERROR = 3
  INFO = 4
  DETAIL = 5
  DEBUG = 6
  NOISY = 7
  PEER_NOTE_PRIVATE_DARKNET_COMMENT = 1
  defaultVerbosity = 3
  ONE_YEAR = 31536000
  fcpVersion = '0.2.5'
  _re_slugify = re.compile(r'(?u)[^\w\s\.-]')
  _re_slugify_multidashes = re.compile(r'(?u)[-\s]+')
  __package__ = 'fcp'
Function Details [hide private]

readdir(dirpath, prefix='', gethashes=False)

source code 

Reads a directory, returning a sequence of file dicts.

TODO: Currently this uses sha1 as hash. Freenet uses 256. But the
      hashes are not used.

Arguments:
  - dirpath - relative or absolute pathname of directory to scan
  - gethashes - also include a 'hash' key in each file dict, being
    the SHA1 hash of the file's name and contents
  
Each returned dict in the sequence has the keys:
  - fullpath - usable for opening/reading file
  - relpath - relative path of file (the part after 'dirpath'),
    for the 'SSK@blahblah//relpath' URI
  - mimetype - guestimated mimetype for file

>>> tempdir = tempfile.mkdtemp()
>>> filename = "test.txt"
>>> testfile = os.path.join(tempdir, filename)
>>> with open(testfile, "w") as f:
...     f.write("test")
>>> correct = [{'mimetype': 'text/plain', 'fullpath': testfile, 'relpath': filename}]
>>> correct == readdir(tempdir)
True
>>> tempdir = tempfile.mkdtemp()
>>> filename = "test"
>>> testfile = os.path.join(tempdir, filename)
>>> with open(testfile, "w") as f:
...     f.write("test")
>>> correct = [{'mimetype': 'application/octet-stream', 'fullpath': testfile, 'relpath': filename}]
>>> correct == readdir(tempdir)
True
>>> res = readdir(tempdir, gethashes=True)
>>> res[0]["hash"] == hashFile(testfile)
True

hashFile(path)

source code 

returns an SHA(1) hash of a file's contents

>>> oslevelid, filepath = tempfile.mkstemp(text=True)
>>> with open(filepath, "w") as f:
...     f.write("test")
>>> hashFile(filepath) == hashlib.sha1("test").hexdigest()
True

sha256dda(nodehelloid, identifier, path=None)

source code 

returns a sha256 hash of a file's contents for bypassing TestDDA

>>> oslevelid, filepath = tempfile.mkstemp(text=True)
>>> with open(filepath, "wb") as f:
...     f.write("test")
>>> print sha256dda("1","2",filepath) == hashlib.sha256("1-2-" + "test").digest()
True

toUrlsafe(filename)

source code 
Make a filename url-safe, keeping only the basename and killing all
potentially unfitting characters.
    
    :returns: urlsafe basename of the file as string.

uriIsPrivate(uri)

source code 

analyses an SSK URI, and determines if it is an SSK or USK private key

for details see https://wiki.freenetproject.org/Signed_Subspace_Key

>>> uriIsPrivate("SSK@~Udj39wzRUN4J-Kqn1aWN8kJyHL6d44VSyWoqSjL60A,iAtIH8348UGKfs8lW3mw0lm0D9WLwtsIzZhvMWelpK0,AQACAAE/")
False
>>> uriIsPrivate("SSK@R-skbNbiXqWkqj8FPDTusWyk7u8HLvbdysyRY3eY9A0,iAtIH8348UGKfs8lW3mw0lm0D9WLwtsIzZhvMWelpK0,AQECAAE/")
True
>>> uriIsPrivate("USK@AIcCHvrGspY-7J73J3VR-Td3DuPvw3IqCyjjRK6EvJol,hEvqa41cm72Wc9O1AjZ0OoDU9JVGAvHDDswIE68pT7M,AQECAAE/test.R1/0")
True
>>> uriIsPrivate("KSK@AIcCHvrGspY-7J73J3VR-Td3DuPvw3IqCyjjRK6EvJol,hEvqa41cm72Wc9O1AjZ0OoDU9JVGAvHDDswIE68pT7M,AQECAAE/test.R1/0")
False
>>> uriIsPrivate("SSK@JhtPxdPLx30sRN0c5S2Hhcsif~Yqy1lsGiAx5Wkq7Lo,-e0kLAjmmclSR7uL0TN901tS3iSx2-21Id8tUp4tyzg,AQECAAE/")
True

parseTime(t)

source code 

Parses a time value, recognising suffices like 'm' for minutes, 's' for seconds, 'h' for hours, 'd' for days, 'w' for weeks, 'M' for months.

>>> endings = {'s':1, 'm':60, 'h':60*60, 'd':60*60*24, 'w':60*60*24*7, 'M':60*60*24*30}
>>> not False in [endings[i]*3 == parseTime("3"+i) for i in endings]
True

Returns time value in seconds

base64decode(enc)

source code 

Decodes a freenet-encoded base64 string back to a binary string

Arguments:

  • enc - base64 string to decode