Socket Server
Server Types
Handlers
  1. #server.py
  2. #!/usr/bin/python
  3.  
  4. import SocketServer
  5.  
  6. import SocketServer
  7.  
  8. class MyTCPHandler(SocketServer.BaseRequestHandler):
  9. def handle(self):
  10. self.data = self.request.recv(1024).strip()
  11. print type(self.request), type(self.client_address), type(self.server)
  12. print 'Client: ', self.client_address
  13. print 'Server: ', self.server
  14. print self.data
  15. # just send back the same data, but upper-cased
  16. self.request.sendall(self.data.upper())
  17.  
  18. if __name__ == "__main__":
  19. HOST, PORT = "localhost", 9999
  20. server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
  21.  
  22. server.serve_forever()
  1. #client.py
  2. #!/usr/bin/python
  3.  
  4. import socket
  5. import sys
  6.  
  7. HOST, PORT = "localhost", 9999
  8.  
  9. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  11.  
  12. try:
  13. data = raw_input("Eneter your message:\n");
  14. sock.connect((HOST, PORT))
  15. sock.sendall(data + "\n")
  16.  
  17. # Receive data from the server and shut down
  18. received = sock.recv(1024)
  19. finally:
  20. sock.close()
  21.  
  22. print "Received: {}".format(received)
Flow Control
  1. #server.py
  2. #!/usr/bin/python
  3.  
  4. import SocketServer
  5. import struct
  6.  
  7. class MyTCPHandler(SocketServer.BaseRequestHandler):
  8.  
  9. def __init__(self, request, client_address, server):
  10. self.n = 0;
  11. SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
  12.  
  13. def handle(self):
  14. s = struct.Struct('f');
  15. while True:
  16. self.data = self.request.recv(s.size)
  17. self.unpacked_data = s.unpack(self.data)
  18. self.n += 1
  19. print 'received: ', self.n, self.unpacked_data[0]
  20.  
  21. if __name__ == "__main__":
  22. HOST, PORT = "localhost", 9999
  23. server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
  24.  
  25. server.serve_forever()
  1. #client.py
  2. #!/usr/bin/python
  3.  
  4. import random
  5. import socket
  6. import sys
  7. import struct
  8.  
  9. HOST, PORT = "localhost", 9999
  10.  
  11. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  13.  
  14. s = struct.Struct('f');
  15. num = 0;
  16.  
  17. sock.connect((HOST, PORT))
  18.  
  19. try:
  20. while True:
  21. n = random.random();
  22. packed = s.pack(n);#pack data into a string
  23. num += 1;
  24. print 'Sent: ', num, n
  25. sock.sendall(packed)
  26. finally:
  27. pass
  28. #sock.close()
Threading and Forking
  1. #server.py
  2. import threading
  3. import SocketServer
  4. import socket
  5.  
  6. class ThreadedEchoRequestHandler(SocketServer.BaseRequestHandler):
  7.  
  8. def handle(self):
  9. # Echo the back to the client
  10. data = self.request.recv(1024)
  11. cur_thread = threading.currentThread()
  12. response = '%s: %s' % (cur_thread.getName(), data)
  13. print 'Received: ', response
  14.  
  15. class ThreadedEchoServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
  16. pass
  17.  
  18. if __name__ == '__main__':
  19. address = ('localhost', 50007) # let the kernel give us a port
  20. server = ThreadedEchoServer(address, ThreadedEchoRequestHandler)
  21. ip, port = server.server_address # find out what port we were given
  22. print ip, port
  23.  
  24. t = threading.Thread(target=server.serve_forever)
  25. #t.setDaemon(True) # don't hang on exit
  26. t.start()
  27. print 'Server loop running in thread:', t.getName()
  28.  
  29. #server.socket.close()
  1. import socket
  2. import threading
  3.  
  4. address = ('localhost', 50007) # let the kernel give us a port
  5.  
  6. # Connect to the server
  7. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8. s.connect(address)
  9.  
  10. # Send the data
  11. message = 'Hello, world'
  12. print 'Sending : "%s"' % message
  13. len_sent = s.send(message)
  14.  
  15. # Clean up
  16. s.close()