import socket
import hashlib
import subprocess
import struct
phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #重用地址
phone.bind(('192.168.20.105',8081))
phone.listen(5)
while 1:
conn,addr=phone.accept()
print('线路是%s' % conn)
print('地址是',addr)
tag = True
while 1:
while tag:
user = conn.recv(1024).decode('utf-8')
pwd = conn.recv(1024).decode('utf-8')
md5_obj = hashlib.md5(user.encode('utf-8'))
s = pwd+user
print(s)
md5_obj.update(s.encode('utf-8'))
pwd = md5_obj.hexdigest()
print(pwd)
if user == 'egon'and pwd=='82bdb4164c14585f32e70ec0a37b3569':
conn.send('right'.encode('utf-8'))
tag = False
break
else:
conn.send('用户名或密码错误,请重新输入!'.encode('utf-8'))
try:
cmd = conn.recv(1024)
res = subprocess.Popen(cmd.decode('utf-8'),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
mes = res.stdout.read()
mesr = res.stderr.read()
ret = struct.pack('i',(len(mes)+len(mesr)))
conn.send(ret)
conn.send(mes)
conn.send(mesr)
except Exception:
break
conn.close()
phone.close()