Updated Python-I2P library 0.91 by sunshine

This commit is contained in:
sunshine
2004-08-02 13:50:21 +00:00
committed by zzz
parent 8d8b6da0bf
commit 8f5d325c4d
13 changed files with 910 additions and 35 deletions

View File

@ -0,0 +1,376 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en"><head><title>User's Guide:i2p.socket - Wikipedia</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="robots" content="index,follow">
<link rel="shortcut icon" href="/favicon.ico">
<style type='text/css'><!--
/*/*/
a.new, #quickbar a.new { color: #CC2200; }
#quickbar { position: absolute; top: 4px; left: 4px; border-right: 1px solid gray; }
#article { margin-left: 152px; margin-right: 4px; }
/* */
//--></style>
</head>
<body bgcolor='#FFFFFF' onload=''>
<h1 class='pagetitle'>User's Guide:i2p.socket</h1><p class='subtitle'>From Python-I2P.
<div class='bodytext'><a name="top"></a>
Module <code >i2p.socket</code > allows Python programs to access the <a href="samproxy.html" class='printable' title ="SAM proxy">SAM proxy</a>. It emulates the Python module <code >socket</code >.
<p>
With this module, a program can send stream data, datagrams, and raw packets across the I2P network.
<p>
<p><table border="0" id="toc"><tr><td align="center">
<b>Table of contents</b></td></tr><tr id='tocinside'><td>
<div style="margin-bottom:0px;">
<A CLASS="internal" HREF="#Sockets">1 Sockets</A><BR>
</div>
<div style="margin-bottom:0px;">
<A CLASS="internal" HREF="#Functions">2 Functions</A><BR>
</div>
<div style="margin-bottom:0px;">
<A CLASS="internal" HREF="#Errors">3 Errors</A><BR>
</div>
<div style="margin-bottom:0px;">
<A CLASS="internal" HREF="#Constants">4 Constants</A><BR>
</div>
</td></tr></table><P>
<h2><a name="Sockets"> Sockets </a></h2>
<p>
class <strong>socket</strong>(session, type, samaddr='127.0.0.1:7656', **kwargs)
<ul ><pre>
Create a new socket. Argument session should be a session
name -- if the name has not yet been used, an I2P
Destination will be created for it, otherwise, the
existing Destination will be re-used. An empty session
string causes a transient session to be created. Argument
type is one of SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW.
I2P configuration keyword arguments:
- in_depth - depth of incoming tunnel (default 2)
- out_depth - depth of outgoing tunnel (default 2)
A single session may be shared by more than one socket, if
the sockets are the same type, and if the sockets are
created within the same Python process. The socket
objects are multithread-safe.
Examples:
&gt;&gt;&gt; a = i2p.socket('Alice', i2p.SOCK_STREAM)
&gt;&gt;&gt; b = i2p.socket('Bob', i2p.SOCK_DGRAM,
in_depth=2, out_depth=5)
The created object behaves identically to a socket from
module socket, with the following exceptions:
* I2P Destinations are used as address arguments [1].
* bind is a no-op: sockets are always bound.
* send* methods send all data and are non-blocking.
A given session name can only be open in a single Python
program at a time. If you need to overcome this
limitation, consider patching I2P.
[1]. Alternatively, a host name can be used as an address.
It will be resolved using hosts.txt.
</pre>
<pre> For details on how to use socket objects, see
<a href="http://www.python.org/doc/current/lib/socket-objects.html" class='printable' title="http://www.python.org/doc/current/lib/socket-objects.html">http://www.python.org/doc/current/lib/socket-objects.html</a>
</pre>
<p>
<pre> See the examples directory for code examples.
</pre>
</ul >
The class <code >socket</code > defines the following properties:
<ul >
<pre> dest - Local I2P Destination of socket
session - Session name
type - Socket type: SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW.
</pre>
</ul >
<p>
The class <code >socket</code > defines the following methods:
<p>
<strong>accept</strong>(self)
<ul ><pre>
Accept an incoming connection. The socket must be type SOCK_STREAM, and
listen() must be called prior to this command. The return value is (conn,
remotedest), where conn is a new socket object made for the connection, and
remotedest is the remote Destination from which the connection was made.
Example:
&gt;&gt;&gt; from i2p import socket
&gt;&gt;&gt; s = socket.socket('Alice', socket.SOCK_STREAM)
&gt;&gt;&gt; s.listen(10)
This prepares the server. Now accept an incoming connection:
&gt;&gt;&gt; c, remotedest = s.accept()
&gt;&gt;&gt; c.send('hello world!')
If accept() is called on a socket that is in non-blocking mode or has a
timeout, i2p.socket.BlockError or i2p.socket.Timeout may be raised. This
indicates that no incoming connection is currently available.
</pre>
</ul >
<strong>bind</strong>(self, address)
<ul ><pre>
Does nothing. Provided for compatibility with the Python socket command
bind(), which binds a server to a port.
</pre>
</ul >
<strong>close</strong>(self)
<ul ><pre>
Closes the socket. It is an error to call any method other than recv() or
recvfrom() on a closed socket. For streams, the receive methods return data
that was received prior to the closing of the socket. For datagram and raw
sockets, the receive methods cannot be used on a closed socket.
</pre>
</ul >
<strong>connect</strong>(self, address)
<ul ><pre>
Connect to a remote dest, identified in local SAM bridge's hosts file as host
'address'.
For example:
&gt;&gt;&gt; s.connect('duck.i2p')
Alternatively, you can use a full base64 Destination:
Example:
&gt;&gt;&gt; s.connect('238797sdfh2k34kjh....AAAA')
If connect() is called on a socket that is in non-blocking mode or has a
timeout, i2p.socket.BlockError or i2p.socket.Timeout may be raised. This
indicates that the connection is still being initiated. Use i2p.select.select()
to determine when the connection is ready.
</pre>
</ul >
<strong>connect_ex</strong>(self, address)
<ul ><pre>
Like connect(), but return any error that is raised. Returns None if no error
is raised.
</pre>
</ul >
<strong>getpeername</strong>(self)
<ul ><pre>
Get the remote Destination associated with the socket. This is equivalent to
s.remotedest, and is provided for compatibility with the Python socket module.
</pre>
</ul >
<strong>getsockname</strong>(self)
<ul ><pre>
Get the local Destination associated with the socket. This is equivalent to
s.dest, and is provided for compatibility with the Python socket module.
</pre>
</ul >
<strong>gettimeout</strong>(self)
<ul ><pre>
Get the timeout value.
</pre>
</ul >
<strong>listen</strong>(self, backlog)
<ul ><pre>
Listen for connections made to the socket. This method must be called before
accept(). The backlog argument specifies the maximum number of queued incoming
connections.
</pre>
</ul >
<strong>makefile</strong>(self, mode='r', bufsize=-1)
<ul ><pre>
Return a file object for the socket. See socket.makefile() in the Python
documentation for more information.
</pre>
</ul >
<strong>recv</strong>(self, bufsize, flags=0)
<ul ><pre>
Receive string data from the socket.
The maximum amount of data to be received is given by bufsize. If bufsize is
zero, this function returns an empty string immediately. If bufsize is nonzero,
this function blocks until at least one character is available for reading. If
the socket has been closed, an empty string is returned as an end of file
indicator.
If recv() is called on a socket that is in non-blocking mode or has a timeout,
i2p.socket.BlockError or i2p.socket.Timeout will be raised if data is not
available within the given timeframe.
For a datagram or raw socket, the first bufsize characters of the packet are
read, and the remainder of the packet is discarded. To read the entire packet,
use bufsize = -1.
For datagram and raw sockets, the packet may originate from any Destination.
Use recvfrom() with datagrams to determine the Destination from which the
packet was received.
The flags argument can be a bitwise OR of MSG_PEEK, MSG_WAITALL, and/or
MSG_DONTWAIT. MSG_PEEK indicates that any data read should not be removed from
the socket's incoming buffer. MSG_WAITALL indicates to wait for exactly bufsize
characters or an error. MSG_DONTWAIT indicates that the recv() command should
not block execution.
</pre>
</ul >
<strong>recvfrom</strong>(self, bufsize, flags=0)
<ul ><pre>
Like recv(), but returns a tuple (data, remoteaddr), where data is the string
data received, and remoteaddr is the remote Destination.
</pre>
</ul >
<strong>send</strong>(self, string, flags=0)
<ul ><pre>
Sends string data to a remote Destination.
For a stream, connect() must be called prior to send(). Once close() is called,
no further data can be sent, and the stream cannot be re-opened.
For datagram and raw sockets, connect() only specifies a Destination to which
packets are sent to. send() will then send a packet to the given Destination.
connect() can be used multiple times.
The send() command never blocks execution. The flags argument is ignored.
</pre>
</ul >
<strong>sendall</strong>(self, string, flags=0)
<ul ><pre>
Identical to send().
</pre>
</ul >
<strong>sendto</strong>(self, string, flags, address)
<ul ><pre>
Send a packet to the given Destination.
Only valid for datagram and raw sockets. The address argument should be either
a name from the hosts file, or a base64 Destination.
The sendto() command never blocks execution. The flags argument is ignored.
</pre>
</ul >
<strong>setblocking</strong>(self, flag)
<ul ><pre>
Set blocking or non-blocking mode for the socket.
If flag is True, any method called on the socket will hang until the method has
completed. If flag is False, all methods will raise i2p.socket.BlockError() if
they cannot complete instantly.
s.setblocking(False) is equivalent to s.settimeout(0); s.setblocking(True) is
equivalent to s.settimeout(None).
</pre>
</ul >
<strong>settimeout</strong>(self, value)
<ul ><pre>
Set a timeout for the socket.
The value argument should be a timeout value in seconds, or None. None is
equivalent to an infinite timeout.
A socket operation will raise a i2p.socket.Timeout if the operation cannot
complete within in the specified time limit.
</pre>
</ul >
<p>
<h2><a name="Functions"> Functions </a></h2>
<p>
Functions defined in module <code >i2p.socket</code >:
<p>
<strong>resolve</strong>(host, samaddr='127.0.0.1:7656')
<ul >
<pre> Resolve I2P host name --&gt; I2P Destination.
Returns the same string if host is already a Destination.
</pre>
</ul >
<p>
<h2><a name="Errors"> Errors </a></h2>
<p>
class <strong>Error</strong>(i2p.Error)
<ul ><pre>
Base class for all SAM errors.
</pre>
</ul >
class <strong>BlockError</strong>(Error)
<ul ><pre>
Socket call would have blocked.
</pre>
</ul >
class <strong>ClosedError</strong>(Error)
<ul ><pre>
A command was used on a socket that closed gracefully.
</pre>
</ul >
class <strong>NetworkError</strong>(Error)
<ul ><pre>
Network error occurred within I2P.
The error object is a 2-tuple: (errtag, errdesc).
errtag is a SAM error string,
errdesc is a human readable error description.
</pre>
</ul >
<p>
<h2><a name="Constants"> Constants </a></h2>
<p>
<strong>Socket types</strong>
<ul ><pre>
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
</pre>
</ul >
<strong>Packet sizes</strong>
<ul ><pre>
MAX_DGRAM = 31744 # Maximum size for datagram packet
MAX_RAW = 32768 # Maximum size for raw packet
</pre>
</ul >
<strong>Flags for recv()</strong>
<ul ><pre>
MSG_DONTWAIT = 128 # Don't block
MSG_PEEK = 2 # Peek at incoming data
MSG_WAITALL = 64 # Wait for all data or error
</pre>
</ul >
<p>
</div>
</body></html>