Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SocketServer module example
01-25-2010, 04:09 PM (This post was last modified: 01-25-2010 04:12 PM by codecaine.)
Post: #1
SocketServer module example
If you just look at python documentation that comes with python it has examples for most modules. All you have to do is copy and paste and modifie it to what you want. Just like I did with this ServerSocket.
This is a server I made. It responds with 3 different things. If you type jeromewantstoknowman the server will say why its me running your server. If you type jeromesayskill it will shutdown the server. If anything else is type is will burp it back to you Smile. Connect to it on telnet on port 6666;

Code:
import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):
    """
    The RequestHandler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def handle(self):
        while True:
            # self.request is the TCP socket connected to the client
            self.data = self.request.recv(1024).strip()
            print "%s wrote:" % self.client_address[0]
            print self.data
            # if cleint data equals jeromewantstoknowman tell the client you are running
            if self.data == "jeromewantstoknowman":
                self.request.send("Why is me running your server jerome!\n")
            #if client data equals jeromesayskill kill the server
            elif self.data == "jeromesayskill":
                server.shutdown();
            else:
                #repeats what they type added newline at the data
                self.request.send("burp: " + self.data + "\n");

if __name__ == "__main__":
    HOST, PORT = "localhost", 6666

    # Create the server, binding to localhost on port 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()


Attached File(s)
.zip  SocketServerExamp.py.zip (Size: 788 bytes / Downloads: 3)
Visit this user's website Find all posts by this user
Quote this message in a reply
01-25-2010, 08:13 PM
Post: #2
RE: SocketServer module example
Ha ha funny arguments "jeromesayskill" and "jeromewantstoknowman" . . .
Great job !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: