#!/usr/bin/python -u """ Pytecache is a GnutellaNet hosts cachter cacher servant. Copyright (C) 2000 Tom Goulet This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA I can be reached at tomg@nova.yi.org. """ import pytella # Hopefully all the functions I need. MAXCONNS = 256 SLEEPINESS = MAXCONNS * 1 import select,time,string def main(s): f = open('pytella.hosts', 'r') conns = [] sleepflag = 0 while 1: for i in conns: if i[1] == -1: pytella.close(i[0]) conns.remove(i) if pytella.ready(s): if len(conns) > MAXCONNS: pytella.close(conns[0][0]) conns.remove(conns[0]) try: tmpconn, tmpaddr = pytella.accept(s) conns.append([tmpconn, 4]) except pytella.AcceptError: pass for i in conns: conns[conns.index(i)] = handleconn(i, f) time.sleep((256-len(conns)) / 256.0) def handleconn(conn, f): if conn[1] == -1: return(conn) elif conn[1] in [4,5,6,7]: if pytella.ready(conn[0]): try: rheader, rpayload = pytella.recv(conn[0]) except pytella.RecvError, message: if message == 'Receive timeout': return(conn) else: conn[1] = -1 pytella.close(conn[0]) return(conn) else: return(conn) guid, func, ttl, hops, length = pytella.parseheader(rheader) if func == 'ping': conn[1] = 256 conn.append(guid) return(conn) if conn[1] == 7: conn[1] = -1 pytella.close(conn[0]) return(conn) else: conn[1] = conn[1] + 1 elif conn[1] == 255: print "foo" elif conn[1] > 255: ttl = 0 hops = 0 host = string.strip(f.readline()) if host == '': f.seek(0) host = string.strip(f.readline()) host = string.split(host) host = (host[0], int(host[1])) try: pytella.sendpong(conn[0], conn[2], ttl, hops, host) except pytella.SendError, message: if message == 'Send timeout': return(conn) else: conn[1] = -1 pytella.close(conn[0]) return(conn) if conn[1] == 512: pytella.close(conn[0]) conn[1] = -1 return(conn) conn[1] = conn[1] + 1 return(conn) else: return(conn) return(conn) s = pytella.listen(('', 6346)) try: main(s) except: s.close() raise