Ejemplo n.º 1
0
 def __init__(self, port, verbose):
     self.prompt = "[Zap Torrent]"
     self.port = port
     self.local_files = ZapFiles()
     self.remote_files = ZapFiles()
     ZapConfig.verbose = verbose
     self.ip = self.get_ip()
     zap_debug_print("my ip is %s, port is %d" % (self.ip, self.port))
     self.broadcast_port = port
     ZapConfig.ip = self.get_ip()
     self.discoverer = FilesLister(port=self.broadcast_port, remote_files=self.remote_files)
     ZapConfig.tcp_port = random.randint(1300, 40000)
     self.tcp_port = ZapConfig.tcp_port
Ejemplo n.º 2
0
class ZapClient:
    def __init__(self, port, verbose):
        self.prompt = "[Zap Torrent]"
        self.port = port
        self.local_files = ZapFiles()
        self.remote_files = ZapFiles()
        ZapConfig.verbose = verbose
        self.ip = self.get_ip()
        zap_debug_print("my ip is %s, port is %d" % (self.ip, self.port))
        self.broadcast_port = port
        ZapConfig.ip = self.get_ip()
        self.discoverer = FilesLister(port=self.broadcast_port, remote_files=self.remote_files)
        ZapConfig.tcp_port = random.randint(1300, 40000)
        self.tcp_port = ZapConfig.tcp_port

    def get_ip(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ip = ""
        try:
            sock.connect(("google.com", 80))
            ip = sock.getsockname()[0]
        except socket.gaierror:
            # if there is no connection to the internet, try getting it by just
            # broadcasting
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            try:
                sock.sendto("HURP DURP", ("<broadcast>", self.port))
                zap_debug_print("self.port is %d" % self.port)
                ip = sock.getsockname()[0]
            except socket.error:
                print >> sys.stderr, ("Sorry, but I can't connect to the network for some reason.")
                print >> sys.stderr, ("Check your network connection and try running ZapTorrent again.")
                sys.exit(2)
        return ip

    def quit(self):
        print("Thanks for using Zap Torrent!")
        sys.exit(0)

    def print_usage(self):
        print("""usage:
quit        #quits the program
name [name] #sets the name of the peer
list        #lists all files available to download from other peers
load [file] #makes a fie availible to peers for download
get [file]  #downloads file""")

    def print_welcome(self):
        print("Welcome to ZapTorrent. Type `usage` for instructions.")

    def run(self):
        self.print_welcome()

        self.discoverer.start()
        #Send something to the discover sock so we can bind it to a port
        self.discoverer.sock.sendto("HURP DURP", ("<broadcast>", self.broadcast_port))
        own_address = self.discoverer.sock.getsockname()
        zap_debug_print("my udp discovery port is", own_address[1])
        ignore_port = own_address[1]
        b = ZapBroadcast(self.broadcast_port, self.local_files,
                self.remote_files, self.ip, ignore_port)
        b.start()

        tcp_server = ZapTCPServer(port=self.tcp_port, host=self.ip, local_files=self.local_files)
        tcp_server.start()

        while True:
            line = raw_input(self.prompt + " ")
            if re.match('^quit$', line):
                self.quit()
            elif re.match('^name (\w+)$', line):
                ZapConfig.name = re.match(r"^name (\w+)$", line).group(1)
                print("Name is now %s" % ZapConfig.name)
            elif re.match('^list$', line):
                self.remote_files.clear()
                query = ZapTorrentProtocolResponse(response_type="files?").as_response()
                s = self.discoverer.sock
                length = 0
                while length < len(query):
                    sent_length = s.sendto(query, ("<broadcast>", self.broadcast_port))
                    length += sent_length
                #now wait for the filesLister to get more info
                print("Waiting for response from peers. . .")
                time.sleep(3)
                for k in self.remote_files.get_files():
                    print("File: %s" % k)
            elif re.match('^load ([\w\._\-/]+)$', line):
                path = re.match('^load ([\w\._\-/]+)$', line).group(1)
                f = ZapFile()
                if f.set_path(path):
                    zap_debug_print("Loaded a local file and it is ", f)
                    self.local_files.add(f)
                    print("File at %s loaded for sharing." % path)
                else:
                    print("File at %s doesn't exist, or it is a directory. Try a different path." % path)
            elif re.match('^get ([\w\.\-]+)$', line):
                filename =  re.match('^get ([\w\.\-]+)$', line).group(1)
                remote_files = self.remote_files.get(filename)
                if remote_files is not None:
                    downloader = ZapDownloader(remote_files, self.local_files)
                    downloader.start()
                    print("Starting to download %s." % filename)
                else:
                    print("No files by that name. Sorry. Try 'list' to update your list of files.")

            else:
                self.print_usage()
                continue