I'm trying to write a python networking program that is capable of recieving arbitrary files, as well as sending them. However, I can't really get it working, though occasionally a string seems to get through in tact. If someone could look through my code and maybe run it and look at the debugger statements I'd really appreciate it.
import SocketServer import cPickle import socket import thread #Listens for connections and accepts all incoming data. class Server(SocketServer.BaseRequestHandler): global list #global list to store incoming data list =  #Just show that the connection worked def setup(self): print self.client_address, 'connected!' self.request.send('hi ' + str(self.client_address) + '\n') def handle(self): print len(list) #debug statement total_data =  #list to contain incoming packets while 1: data = self.request.recv(1024) #recieve data if not data: break #stop trying to recieve data if no data is sent if data.strip() == "I need to get my drink on.": #if the token is found, send out the last element in the list if len(list) > 0: self.request.send(list.pop()) #if the list has at least one element, pop else: self.request.send('None') #if the list is empty, send nothing return #exit out after sending the file total_data.append(data) #add incoming data to the list obj = ''.join(total_data) #concatenate all the data recieved if obj != None: #as long as obj isn't null, unpickle it and store at the beginning of the list pickle = cPickle.loads(obj) list.insert(0,pickle) #This class creates an instance of the listening server and provides a means to send and retrieve items. class serverInterface: def __init__(self,address,port): server = SocketServer.ThreadingTCPServer((address, port), Server) thread.start_new_thread(server.serve_forever,()) self.address = address self.port = port def sendItem(self, address,port,object): client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) client.connect ( ( address, port ) ) obj = object pickle = cPickle.dumps(obj) client.send(pickle) client.close() def getNextItem(self): client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) client.connect ( ( self.address, self.port ) ) client.send("I need to get my drink on.") total_data =  while 1: data = client.recv(1024) if not data:break total_data.append(data) obj = ''.join(total_data) client.close() return obj
And for the test code I tried
from Network import serverInterface x = serverInterface('localhost',5000) x.sendItem('localhost',5000,"This") x.sendItem('localhost',5000,"Is") x.sendItem('localhost',5000,"WORKING!!!") print x.getNextItem() print x.getNextItem() print x.getNextItem()
I seem to get quite a few errors on line 20,
data = self.request.recv(1024) #recieve data
Thank you for any help you can give.