initial import of Connelly's public domain I2P python lib

This commit is contained in:
jrandom
2004-07-21 07:42:29 +00:00
committed by zzz
parent 8603250d73
commit 5214436d18
39 changed files with 4533 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
# -----------------------------------------------
# datagram.py: Datagram client
# -----------------------------------------------
from i2p import sam
dest = sam.resolve('yourserver.i2p')
S = sam.socket('Bob', sam.SOCK_DGRAM)
S.sendto('Hello packet', 0, dest)
# Get packet up to 1000 bytes -- the rest is discarded.
(data, dest) = S.recvfrom(1000)
print data

View File

@@ -0,0 +1,20 @@
# ---------------------------------------------------
# datagram_noblock.py: Non-blocking datagram server
# ---------------------------------------------------
from i2p import sam
import time
S = sam.socket('Eve', sam.SOCK_DGRAM)
print 'Serving at:', S.dest
S.setblocking(False)
while True:
try:
(data, dest) = S.recvfrom(1000) # Read packet
print 'Got', data, 'from', dest
S.sendto('Hi client!', 0, dest)
except sam.BlockError: # No data available
pass
time.sleep(0.01) # Reduce CPU usage

View File

@@ -0,0 +1,14 @@
# -----------------------------------------------
# datagram_server.py: Datagram server
# -----------------------------------------------
from i2p import sam
S = sam.socket('Eve', sam.SOCK_DGRAM)
print 'Serving at:', S.dest
while True:
(data, dest) = S.recvfrom(1000) # Read packet
print 'Got', data, 'from', dest
S.sendto('Hi client!', 0, dest)

View File

@@ -0,0 +1,33 @@
# -----------------------------------------------
# dos.py: Noneffective denial of service tool
# -----------------------------------------------
from i2p import sam
import threading, sys
def dos_stream(dest):
"""Perform a DOS attack on a stream server."""
dest = sam.resolve(dest)
# DOS code, runs in n separate threads.
def f():
while True:
S = sam.socket(dest, sam.SOCK_STREAM)
S.connect(dest)
S.send('GET / HTTP/1.0\r\n\r\n')
S.close()
# Start up the threads.
for i in range(128):
T = threading.Thread(target=f)
T.start()
def syntax():
print "Usage: python dos.py Destination"
if __name__ == '__main__':
if len(sys.argv) == 2:
dos_stream(sys.argv[1])
else:
syntax()

View File

@@ -0,0 +1,14 @@
Examples:
datagram.py - Datagram client
datagram_noblock.py - Non-blocking datagram server
datagram_server.py - Blocking datagram server
dos.py - Denial of service tool
raw.py - Raw client
raw_noblock.py - Non-blocking raw server
raw_server.py - Raw server
stream.py - Stream client
stream_eepget.py - Get an eepsite using sockets
stream_noblock.py - Non-blocking stream server
stream_server.py - Blocking stream server

View File

@@ -0,0 +1,10 @@
# -----------------------------------------------
# raw.py: Raw client
# -----------------------------------------------
from i2p import sam
dest = sam.resolve('yourserver.i2p') # Send to dest
S = sam.socket('Carol', sam.SOCK_RAW)
S.sendto('Hello packet', 0, dest)

View File

@@ -0,0 +1,19 @@
# ---------------------------------------------------
# raw_noblock.py: Non-blocking raw server
# ---------------------------------------------------
from i2p import sam
import time
S = sam.socket('Eve', sam.SOCK_RAW)
print 'Serving at:', S.dest
S.setblocking(False)
while True:
try:
data = S.recv(1000) # Read packet
print 'Got', data
except sam.BlockError: # No data available
pass
time.sleep(0.01) # Reduce CPU usage

View File

@@ -0,0 +1,13 @@
# -----------------------------------------------
# raw_server.py: Raw server
# -----------------------------------------------
from i2p import sam
S = sam.socket('Eve', sam.SOCK_RAW)
print 'Serving at:', S.dest
while True:
data = S.recv(1000) # Read packet
print 'Got', data

View File

@@ -0,0 +1,11 @@
# -----------------------------------------------
# stream.py: Simple stream client
# -----------------------------------------------
from i2p import sam
S = sam.socket('Alice', sam.SOCK_STREAM)
S.connect('duck.i2p')
S.send('GET / HTTP/1.0\r\n\r\n') # Send request
print S.recv(1000) # Read up to 1000 bytes

View File

@@ -0,0 +1,17 @@
# -----------------------------------------------
# stream_eepget.py: Get an eepsite using sockets
# -----------------------------------------------
from i2p import sam
S = sam.socket('Alice', sam.SOCK_STREAM)
S.connect('duck.i2p')
S.send('GET / HTTP/1.0\r\n\r\n') # Send request
f = S.makefile() # File object
while True: # Read header
line = f.readline().strip() # Read a line
if line == '': break # Content begins
print f.read() # Read file object

View File

@@ -0,0 +1,39 @@
# -----------------------------------------------
# stream_noblock.py: Non-blocking stream server
# -----------------------------------------------
import i2p
from i2p import sam
import thread, time
S = sam.socket('Dave', sam.SOCK_STREAM)
S.listen(10) # Queue up to 10 connections
S.setblocking(False) # Non-blocking
print 'Serving at:', S.dest
def handle_connection(C):
"""Handle a single connection in a thread of its own."""
try:
f = C.makefile() # File object
req = f.readline() # Read HTTP request
s = '<h1>Hello!</h1>' # String to send back
f.write('HTTP/1.0 200 OK\r\nContent-Type: text/html' +
'\r\nContent-Length: ' + str(int(len(s))) + '\r\n\r\n' + s)
f.close() # Close file
C.close() # Close connection
except sam.Error, e:
# Recover from SAM errors
print 'Warning:', str(e)
while True:
try:
(C, remotedest) = S.accept() # Get a connection
thread.start_new_thread(handle_connection, (C,))
except sam.BlockError:
# Ignore 'command would have blocked' errors
pass
time.sleep(0.01) # Reduce CPU usage

View File

@@ -0,0 +1,28 @@
# -----------------------------------------------
# stream_server.py: Simple stream server
# -----------------------------------------------
import i2p
from i2p import sam
S = sam.socket('Dave', sam.SOCK_STREAM)
S.listen(10) # Queue up to 10 connections
print 'Serving at:', S.dest
while True:
try:
(C, remotedest) = S.accept() # Get a connection
f = C.makefile() # File object
req = f.readline() # Read HTTP request
s = '<h1>Hello!</h1>' # String to send back
f.write('HTTP/1.0 200 OK\r\nContent-Type: text/html' +
'\r\nContent-Length: ' + str(int(len(s))) + '\r\n\r\n' + s)
f.close() # Close file
C.close() # Close connection
except sam.Error, e:
# Recover from SAM errors
print 'Warning:', str(e)