socket

Contributor:bestlzk Type:代码 Date time:2020-05-31 02:05:55 Favorite:5 Score:0
返回上页 Report
请选择举报理由:




Collection Modify the typo
import threading
import subprocess
import socket
import time
import json
def exec(command):
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = p.stdout.read().decode('utf8')
errinfo = p.stderr.read().decode('utf8')
while p.poll() == None:
time.sleep(1)
return {'result': result, 'errinfo': errinfo, 'code': p.poll() }
def one_client(conn, addr):
# 认证
print(addr, 'loging...')
info = json.loads(conn.recv(1024).decode('utf8'))
if info['username'] in users.keys() and users[info['username']] == info['password']:
print(addr, 'auth seccess')
conn.sendall('auth seccess'.encode('utf8'))
else:
print(addr, 'auth failed')
conn.sendall('auth failed'.encode('utf8'))
conn.close()
return
# 命令执行
while True:
command = conn.recv(1024).decode('utf8')
print(addr, command)
if command == 'exit':
conn.close()
return
result = json.dumps(exec(command.encode('utf8')))
conn.sendall(result.encode('utf8'))
if __name__ == '__main__':
users = {'root': 'root'}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 2333))
s.listen(5)
while True:
conn, addr = s.accept()
threading.Thread(target=one_client, args=(conn, addr,)).start()
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
Hot degree:
Difficulty:
quality:
Description: the system according to the heat, the difficulty, the quality of automatic certification, the certification of the article will be involved in typing!

This paper typing ranking TOP20

登录后可见

用户更多文章推荐