Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. from socket import *
  2. import time
  3. import sys
  4. from subprocess import Popen, PIPE
  5. import select
  6.  
  7. MSGLEN = 0
  8. host = "localhost"
  9. port = int(sys.argv[1])
  10.  
  11. CONNECTION_LIST = []
  12. server_socket = socket()
  13. server_socket.bind((host,port))
  14. server_socket.listen(10)
  15.  
  16. CONNECTION_LIST.append(server_socket)
  17. print "Starting port %d"%port
  18.  
  19.  
  20. #conn, address = sock.accept()
  21.  
  22. def sendMes(conn,mes):
  23. count = 0
  24. while count < MSGLEN:
  25. sent = conn.send(mes[count:])
  26. if sent ==0:
  27. raise RuntimeError("socket connection broken")
  28. count = count + sent
  29.  
  30. def recvMes(conn):
  31. rs = ""
  32. bytes_recd = 0
  33. while bytes_recd < MSGLEN:
  34. temp = conn.recv(min(MSGLEN - bytes_recd,8))
  35. if temp =='':
  36. raise RuntimeError("socket connection broken")
  37. rs += temp
  38. bytes_recd += len(temp)
  39. return rs
  40.  
  41. def execute(mes):
  42. proc_list = []
  43. Error = ""
  44. if " " in mes:
  45. try:
  46. shell= mes.split(" ")[0]
  47. argv = mes.split(" ")[1]
  48. sub_proc = Popen([shell,argv], shell=False, stdout=PIPE)
  49. except:
  50. Error = "Err0r"
  51. else:
  52. try:
  53. sub_proc = Popen(mes,shell=False,stdout=PIPE)
  54. except:
  55. Error = "Err0r"
  56. if Error != "Err0r":
  57. for line in sub_proc.stdout:
  58. proc_list.append(line)
  59. rs = "".join(proc_list)
  60. return rs
  61. else:
  62. return "Command not correct!"
  63.  
  64. while True:
  65. read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
  66. for sock in read_sockets:
  67. if sock == server_socket:
  68. conn, address = server_socket.accept()
  69. CONNECTION_LIST.append(conn)
  70. print "Client (%s, %s) connected" % address
  71. else:
  72. data = conn.recv(8)
  73. if data:
  74. MSGLEN = int (data)
  75. print MSGLEN
  76. conn.send("Ok")
  77. Message = recvMes(conn)
  78. print Message
  79. text = execute(Message)
  80. print text
  81. lenMes = len(text)
  82. conn.send(str(lenMes))
  83. conn.recv(8)
  84. sendMes(conn,text)
  85.  
  86. else:
  87. sock.close()
  88. CONNECTION_LIST.remove(sock)
  89. continue
  90.  
  91.  
  92.  
  93. #conn.send("Ok")
  94.  
  95. server_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement