Dear Tutors,
How would I send a file from one computer to another. I have
modules which can send simple objects, such as dictionaries with simple
objects in it. They can't send files thou. Please help.
Sincerely,
Me
import SocketServer, cPickle
def echo(self): #the default handling function
print 'Recieved %s from %s. Echoing back.' % (self.data,
self.client_address)
self.send(self.data) #echo
print self.server
class BreakOutError(Exception): pass #an error for breaking out
def create_handler(handle_func=echo): #creates an handler
class Handler(SocketServer.BaseRequestHandler): #the handler
'''A handler which calls %s in the handle method.'''%handle_func
def handle(self): #the handle method
self.data = self.request.recv(1024) #the data
handle_func(self) #call the handle method giving self
def send(self, data): self.request.send(data) #send data
def shut_down(self): #if you want to stop server
'''End server loop'''
self.server.shut_self_down = True #set shut down to true
raise BreakOutError #raise error
return Handler
def pickle_echo(self):
print 'Recived %s, a %s, from %s. Echoing back.' % (self.data,
type(self.data), self.client_address)
self.send(self.data)
def pickle_HC(handle_func = pickle_echo): #a function that returns a handler
that can send a recvieve more than strings if to a pickle client
base = create_handler(handle_func) #make a base
class pickle_Handler(base):
'''A handler which call %s in the handle method and pickle/unpickles on
the way in and out.'''%handle_func
def handle(self):
self.data = cPickle.loads(self.request.recv(1024)) #unpickle data
handle_func(self)
def send(self, data): base.send(self, cPickle.dumps(data))
return pickle_Handler
class EB_Server(SocketServer.TCPServer):
'''Error Breakout Server
When you use server.shutdown, it waits for the handle to end, but this is the
only place to do stuff.
So the error breakout server uses error to break out of a server loop, which
will be caught outside.'''
def __init__(self, address, handler):
'''Init, set self.shutdown to False'''
SocketServer.TCPServer.__init__(self, address, handler) #make a handler
from a function
self.shut_self_down = False #When an error is raised and handle_error
catches it, it will check if its an breakout error
def handle_error(self, request, address):
'''Test to see if shutting down.'''
if self.shut_self_down: raise BreakOutError #if shutdown, raise
breakout error
else: SocketServer.TCPServer.handle_error(self, request, address) #If
not, do normal error handling
def run(handle_func = echo, host='localhost', port=1024): #init function
try: EB_Server((host, port), create_handler(handle_func)).serve_forever()
#serve forever
except BreakOutError: pass #If it tries to break out, catch it before it
destroys us at the main level
def pickle_run(handle_func = pickle_echo, host='localhost', port = 1024):
try: EB_Server((host, port), pickle_HC(handle_func)).serve_forever()
except BreakOutError: pass
if __name__ == '__main__': run() #if run directly, run server
import socket, cPickle
class NotConnectedError(Exception): pass #A ClosedError
class Client(object): #client object
def __init__(self, host = 'localhost', port = 1024, timeout = None):
self.host = host #set attributes
self.port = port
self.timeout = timeout
self.closed = False
def send(self, data): #send data
try: self.__sock.close() #close old socket
except AttributeError: pass #If there isn't a socket, don't worry about
it
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #make a
new one
self.__sock.connect((self.host, self.port))
self.closed = False
self.__sock.settimeout(self.timeout)
self.__sock.send(data)
def recv(self): #receive data
if self.closed: raise NotConnectedError, 'The socket isn\'t connected.'
#if closed, raise error
return self.__sock.recv(1024)
def close(self): #close sock
self.__sock.close()
self.closed = True
def connect(self, host, port): #connect or reconnect
self.__sock.connect((host, port))
self.closed = True #set closed to false
def __get_timeout(self):
try: return self.__sock.gettimeout()
except AttributeError: return self.__timeout #sock may not exist, so do
predefined
def __set_timeout(self, timeout):
self.__timeout = timeout
try: self.__sock.settimeout(self.__timeout)
except AttributeError: pass #sock may not exist
timeout = property(__get_timeout, __set_timeout)
class Pickle_Client(Client): #a client to send objects, not just strings
def send(self, data):
Client.send(self, cPickle.dumps(data)) #pickle the object first
def recv(self):
return cPickle.loads(Client.recv(self)) #unpickles object first
##if __name__ == '__main__':
## client = Client()
## while True:
## client.send(raw_input('What do you want to send? '))
## print client.recv()
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor