Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Simple Server

By: a guest on Jan 18th, 2014  |  syntax: Python  |  size: 1.68 KB  |  views: 44  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2.  
  3. """very simple time server with limited argument options H.M.S.MS"""
  4.  
  5. import socket
  6. from datetime import datetime
  7.  
  8. __author__ = 'Peter McKinney'
  9. __date__ = '14/01/14'
  10. __copyright__ = 'Copyright 2014, Peter McKinney'
  11. __license__ = "FreeBSD"
  12. __maintainer__ = 'Peter McKinney'
  13. __email__ = 'peter.mckinney21@gmail.com'
  14. __status_ = 'Prototype'
  15.  
  16. HOST = ''                #''socket.gethostname()
  17. PORT = 7286              # Arbitrary non-privileged port
  18. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19. s.bind((HOST, PORT))
  20. s.listen(5)
  21.  
  22. print 'Server started on Port <', PORT, '>'
  23.  
  24. while 1:
  25.     conn, addr = s.accept()
  26.     print 'Connected by', addr
  27.     data = conn.recv(1024)
  28.  
  29.     print 'with request for -> ', data
  30.     return_string = ""
  31.  
  32.     if data.startswith("[EXIT]"):
  33.         break
  34.  
  35.     elif data.startswith("[TIME]"):
  36.         cmd = data.split(" ")
  37.         if len(cmd) > 1:
  38.             cmd = cmd[1].split(".")
  39.  
  40.             if len(cmd) > 0:
  41.                 for i in xrange(len(cmd)):
  42.                     if cmd[i] == "H":
  43.                         return_string += str(datetime.now().hour)
  44.                     elif cmd[i] == "M":
  45.                         return_string += str(datetime.now().minute)
  46.                     elif cmd[i] == "S":
  47.                         return_string += str(datetime.now().second)
  48.                     elif cmd[i] == "MS":
  49.                         return_string += str(datetime.now().microsecond)
  50.  
  51.                     if i+1 < len(cmd):
  52.                         return_string += "."
  53.  
  54.         else:
  55.             return_string = str(datetime.now().time())
  56.  
  57.     conn.send(return_string)
  58.     conn.close()
  59.        
  60. s.close()
clone this paste RAW Paste Data