Mostra notizie nelle discussioni
Avatar
Scritto da
Non Autenticato
il 2008-03-01 01:40:00.0 alle 2008-03-01 01:40:00.0
150 righe sono meglio di 15

#!/usr/bin/python
#
# tinyswarm.py -- P2P swarming in <150 lines(*)

# Copyright (c) 2004, Brian St. Pierre <brian@bstpierre.org>

# This will swarm a file using a small, known network of peers. No
# attempt at peer discovery is made. Inspired by tinyp2p, but 10x
# longer -- and 10x easier to understand. Only one file may be swarmed
# over this network at a time.

# Probably works with any python2. Tested on 2.2.[123], 2.3.3.

# Usage:
# tinyswarm.py <filename> <peer-ip-address> [<peer-ip-address> ...]
#
# If 'filename' is '-', we will connect to each peer and start
# swarming from whoever has pieces (hunks) of the file. Otherwise, we will
# offer hunks to anyone that connects to us.
#
# You'll need to have port 8877 open on your firewall, or adjust
# LISTEN_PORT below.

# THIS IS INCREDIBLY INSECURE. If someone knows your peer address,
# they may be able to force you to swarm a file.

# This code is not thread-safe, which causes a bug about 1 in 10 times
# (in my testing -- YMMV) when one of the peers nears completion of
# receiving the file, and occasionally when starting up.

# Use this program however you want, but THERE IS ABSOLUTELY NO
# WARRANTY for this program. If you break it, you get to keep both
# pieces.

# (*) 150 lines doesn't count comments, whitespace, or docstrings.

import os
import random
import socket
import sys
import threading
import time

LISTEN_PORT = 8878
TINYSWARM_VERSION = 0.1

class SwarmFile:
    '''A file to be swarmed. Hunks are downloaded and then made
    available to other peers.'''

    _HUNK_SIZE = 1400

    def __init__(self, filename):
        self._name = ''
        self._hunks = []
        if filename != '-':
            self._name = os.path.basename(filename)
            self._hunkify(open(filename))
        return

    def _hunkify(self, f):
        '''Split the file into roughly equal sized hunks. (The last
        hunk may be truncated.)'''
        hunk = f.read(self._HUNK_SIZE)
        while hunk != '':
            self._hunks.append(hunk)
            hunk = f.read(self._HUNK_SIZE)
        return

    def name(self):
        '''Get the name of the file.'''
        return self._name

    def set_name(self, name):
        '''Sets the name of the file.'''
        assert(name != '')
        self._name = name
        return

    def num_hunks(self):
        '''Return the number of hunks.'''
        return len(self._hunks)

    def alloc_hunks(self, count):
        '''Create space for all of the hunks.'''
        if len(self._hunks) == 0:
            self._hunks = [None] * count
        else:
            assert(len(self._hunks) == count), (len(self._hunks), count)
        return

    def empty_hunks(self):
        '''Return the number of missing hunks.'''
        return self._hunks.count(None)

Pagina generata in 6 millisecondi

Utenti attivi: 33 nell'ultimo minuto, 92 negli ultimi cinque minuti, 152 negli ultimi 15 minuti