SocketServer class examples

Discussion in 'Python' started by Adil Hasan, Dec 11, 2004.

  1. Adil Hasan

    Adil Hasan Guest

    Hello Fred,
    I just ran across your question. I think that the following
    code will work:

    ----- SERVER CODE ------
    import SocketServer
    import time
    class GreetingHandler(SocketServer.BaseRequestHandler):
    '''Class to handle sending a greeting to a client
    '''
    def handle(self):
    '''Method to actually handle the greeting
    '''
    print 'handling the request'
    line = self.request.recv(bufsize)
    name = ""
    if ('NAME:' in line):
    name = line.split('NAME:')[1]
    self.request.sendall('hello there %s \n' % name)

    def manageServer():
    '''Function to manage the running of the server and handling of
    requests
    '''
    servObj = SocketServer.TCPServer(('localhost',6099),GreetingHandler)
    print 'Starting the server...'
    servObj.serve_forever()

    if __name__ == '__main__':
    manageServer()

    ----- CLIENT CODE -----
    #!/usr/bin/env python
    import socket
    def manageClient():
    '''Function to manage the sending of request to the server
    '''
    server = 'localhost'
    port = 6099
    servObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    servObj.connect((server, port))
    servObj.send('NAME: An Elephant\r\n')
    resp = servObj.recv(8192)
    print resp
    servObj.close()

    if __name__ == '__main__':
    manageClient()



    I think that the problem with the rfile and wfile methods in the
    StreamRequestHandler class is that it's trying to send on the
    same socket as it's reading data on so they are blocking each
    other. I think that this is a problem in the StreamRequestHandler
    class.

    adil

    -----Original Message-----
    From: Frederick Grim [mailto:fgrim at norby.dyndns.org]
    Sent: 18 July 2004 19:34
    To: python-list at python.org
    Subject: SocketServer class examples

    Howdy group,
    So I am in the middle of using the socketserver class from the std
    library and have run into a problem that probably reveals my
    misunderstanding of sockets. I have a class defined like so:

    class tcp_listener(SocketServer.ThreadingTCPServer):
    def __init__(self, addr, port):
    SocketServer.ThreadingTCPServer.__init__(self, \
    (addr, port), Daemon.request_handler)

    """ Yes I realize the above is silly and redundant """

    And a request handler in Daemon that has a handle function that works like
    so:
    def __req_handle(self, req):
    """ Do stuff with req and return a response afterwards """
    return response

    def handle(self);
    while True:
    input = self.rfile.readline()
    request = input
    while input and not re.search('EOF$', input):
    input = self.rfile.readline()
    request += input

    self.wfile.write(self.__req_handle(request))

    The client end looks almost identical to the example in the python docs.
    So
    the problem here is that this code doesn't work. Using tcpdump I can tell
    that the client is sending to the server properly but the server is never
    responding. Or when it tries to respond it gets stuck in the write.
    What's
    going on here. I can't seem to find a single example of how to use this
    class on the client and server side and I don't want to use twisted
    (because
    I should understand how this works instead of relying on canned software).
    Does anyone know where I can find an example of a functioning SocketServer
    and client? Google seems to help nought here.
    Thanks,
    Fred
     
    Adil Hasan, Dec 11, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Janusz
    Replies:
    4
    Views:
    1,125
    Janusz
    Jun 1, 2004
  2. Priya Kheterpal

    bad class file: D:\examples\Hello.class

    Priya Kheterpal, Aug 8, 2004, in forum: Java
    Replies:
    0
    Views:
    672
    Priya Kheterpal
    Aug 8, 2004
  3. lebo
    Replies:
    4
    Views:
    1,100
    Peter Hansen
    Jun 24, 2003
  4. Milo K. Piffenpauffer

    Examples and Tutorials of SocketServer?

    Milo K. Piffenpauffer, Dec 13, 2003, in forum: Python
    Replies:
    4
    Views:
    484
    Fredrik Lundh
    Dec 14, 2003
  5. Frederick Grim

    SocketServer class examples

    Frederick Grim, Jul 18, 2004, in forum: Python
    Replies:
    0
    Views:
    353
    Frederick Grim
    Jul 18, 2004
Loading...

Share This Page