Hey all,
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
					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)

#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)
		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)

	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
		obj = ''.join(total_data)

		return obj

And for the test code I tried

from Network import serverInterface

x = serverInterface('localhost',5000)
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.

I tried to test it, but I don't have a Network module, nor could I find such on Google. Where is that module coming from?


Network is just the name of the file, it can be anything though.