Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import *
- import time
- import sys
- from subprocess import Popen, PIPE
- import select
- MSGLEN = 0
- host = "localhost"
- port = int(sys.argv[1])
- CONNECTION_LIST = []
- server_socket = socket()
- server_socket.bind((host,port))
- server_socket.listen(10)
- CONNECTION_LIST.append(server_socket)
- print "Starting port %d"%port
- #conn, address = sock.accept()
- def sendMes(conn,mes):
- count = 0
- while count < MSGLEN:
- sent = conn.send(mes[count:])
- if sent ==0:
- raise RuntimeError("socket connection broken")
- count = count + sent
- def recvMes(conn):
- rs = ""
- bytes_recd = 0
- while bytes_recd < MSGLEN:
- temp = conn.recv(min(MSGLEN - bytes_recd,8))
- if temp =='':
- raise RuntimeError("socket connection broken")
- rs += temp
- bytes_recd += len(temp)
- return rs
- def execute(mes):
- proc_list = []
- Error = ""
- if " " in mes:
- try:
- shell= mes.split(" ")[0]
- argv = mes.split(" ")[1]
- sub_proc = Popen([shell,argv], shell=False, stdout=PIPE)
- except:
- Error = "Err0r"
- else:
- try:
- sub_proc = Popen(mes,shell=False,stdout=PIPE)
- except:
- Error = "Err0r"
- if Error != "Err0r":
- for line in sub_proc.stdout:
- proc_list.append(line)
- rs = "".join(proc_list)
- return rs
- else:
- return "Command not correct!"
- while True:
- read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
- for sock in read_sockets:
- if sock == server_socket:
- conn, address = server_socket.accept()
- CONNECTION_LIST.append(conn)
- print "Client (%s, %s) connected" % address
- else:
- data = conn.recv(8)
- if data:
- MSGLEN = int (data)
- print MSGLEN
- conn.send("Ok")
- Message = recvMes(conn)
- print Message
- text = execute(Message)
- print text
- lenMes = len(text)
- conn.send(str(lenMes))
- conn.recv(8)
- sendMes(conn,text)
- else:
- sock.close()
- CONNECTION_LIST.remove(sock)
- continue
- #conn.send("Ok")
- server_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement