On 10/3/2010 7:20 PM, Chris King wrote:
 On 10/2/2010 3:40 PM, Evert Rol wrote:
Dear Tutors,
I have attached my 2 programs for networking. It uses socket and SocketServer, but it just simplifies it even more. The problem is it won't work. The Client raises the error, (with trace back)
Traceback (most recent call last):
File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in<module>
     print client.recv()
File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv
     return self.__sock.recv(1024)
error: [Errno 10053] An established connection was aborted by the software in your host machine The server seems to get an error of some sort, but also seems to catch and print it. It prints
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 1424)
Traceback (most recent call last):
File "C:\Python26\lib\SocketServer.py", line 281, in _handle_request_noblock
     self.process_request(request, client_address)
   File "C:\Python26\lib\SocketServer.py", line 307, in process_request
     self.finish_request(request, client_address)
   File "C:\Python26\lib\SocketServer.py", line 320, in finish_request
     self.RequestHandlerClass(request, client_address, self)
TypeError: 'NoneType' object is not callable
----------------------------------------
I look at both of the documentations of socket and SocketServer, but I couldn't firgue it out. I don't know much about networking. Please Help
I don't know much about networking, less so about it on Windows; also, I've only scanned quickly through the server script, but I notice your create_handler() function doesn't return anything. I guess it needs to return the Handler class. The example in the Python docs doesn't use a factory function, but instead directly puts the class as the second argument to TCPServer. So the second argument needs to be a class, but since your create_handler() function returns nothing, you presumably get this NoneType exception.

   Evert

Yeah, that could be a problem. It should return the class.
Now it won't send more than once. The Error the clients raises is:
Traceback (most recent call last):
File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in <module>
    print client.recv()
File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv
    return self.__sock.recv(1024)
error: [Errno 10053] An established connection was aborted by the software in your host machine The server is blissfully unaware of its crashing clients, and keeps on going to help more clients to there dooms. The client can receive data multiple times, but just can send twice. Please help.
import SocketServer

def echo(self): #the default handling function
    print 'Recieved %s from %s. Echoing back.' % (self.data, self.client)
    self.client.send(self.data) #echo

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
            self.client = self.request #the client
            handle_func(self) #call the handle method giving self
            
        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

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

if __name__ == '__main__': run() #if run directly, run server
import socket

class Client(object): #client object
    def __init__(self, host = 'localhost', port = 1024, timeout = None):
        self.host = host #set attributes
        self.port = port
        self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Make a sock
        self.connect(self.host, self.port)
        self.timeout = timeout
        
    def send(self, data): #send data
        if self.closed: raise NotConnectedError, 'The socket isn\'t connected.' #if closed, raise error
        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 = False #set closed to false
    def __get_timeout(self): return self.__sock.gettimeout()
    def __set_timeout(self, timeout): self.__sock.settimeout(timeout)
    timeout = property(__get_timeout, __set_timeout)
    

class __NotConnectedError(Exception): pass #A ClosedError

if __name__ == '__main__':
    client = Client()
    while True:
        client.send(raw_input('What do you want to send? '))
        print client.recv()
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to