Commit 6a623a6d authored by ibuler's avatar ibuler

[Update] Change select to selector

parent 6f072d57
......@@ -37,7 +37,7 @@ class Coco:
def __init__(self, name=None, root_path=None):
self.config = self.config_class(BASE_DIR, defaults=self.default_config)
self.sessions = []
self.connections = []
self.clients = []
self.ws = None
self.root_path = root_path
self.name = name
......@@ -96,6 +96,16 @@ class Coco:
print("Grace shutdown the server")
self.sshd.shutdown()
def add_client(self, client):
self.clients.append(client)
print("%s add client, now %d s" % (self.name, len(self.clients)))
def remove_client(self, client):
self.clients.remove(client)
client.send("Closed by server")
client.close()
print("%s remove client, now %d s" % (self.name, len(self.clients)))
def monitor_session(self):
pass
#coding: utf-8
# coding: utf-8
import socket
import paramiko
......@@ -34,7 +35,6 @@ class ProxyServer:
pass
def get_server_conn(self, asset, system_user):
self.ssh = ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
......
......@@ -47,6 +47,8 @@ class InteractiveServer:
r, w, x = select.select([self.client], [], [])
if self.client in r:
data = self.client.recv(10)
if len(data) == 0:
self.app.remove_client(self.client)
# Client input backspace
if data in char.BACKSPACE_CHAR:
# If input words less than 0, should send 'BELL'
......@@ -80,6 +82,13 @@ class InteractiveServer:
input_data.append(data)
def dispatch(self, opt):
print(opt)
if opt in ['q', 'Q']:
self.app.remove_client(self.client)
return
else:
self.client.send("hello")
asset = Asset(id=1, hostname="123.57.183.135", ip="123.57.183.135", port=8022)
system_user = SystemUser(id=2, username="web", password="redhat123", name="web")
self.connect(asset, system_user)
......
......@@ -49,7 +49,7 @@ class SystemUser(Decoder):
name = ""
username = ""
password = ""
private_key = ""
private_key = None
def __str__(self):
return self.name
......
......@@ -51,7 +51,7 @@ class Session:
while self.running:
try:
r, w, x = select.select([self.client + self.server]
+ self.watchers + self.sharers, [], [])
+ self.sharers, [], [])
for sock in r:
if sock == self.server:
......@@ -67,8 +67,6 @@ class Session:
watcher.send("%s close the session" % self.client)
self.close()
self.server.send(data)
elif sock in self.watchers:
sock.send("WARN: Your didn't have the write permission\r\n")
elif sock in self.sharers:
data = sock.recv(BUF_SIZE)
if len(data) == 0:
......
......@@ -61,13 +61,11 @@ class SSHServer:
try:
sock, addr = self.sock.accept()
logger.info("Get ssh request from %s: %s" % (addr[0], addr[1]))
if len(self.app.connections) >= max_conn_num:
if len(self.app.clients) >= max_conn_num:
sock.close()
logger.warning("Arrive max connection number %s, "
"reject new request %s:%s" %
(max_conn_num, addr[0], addr[1]))
else:
self.app.connections.append((sock, addr))
thread = threading.Thread(target=self.handle, args=(sock, addr))
thread.daemon = True
thread.start()
......@@ -101,6 +99,7 @@ class SSHServer:
sys.exit(2)
client = Client(chan, addr, request.user)
self.app.add_client(client)
self.dispatch(request, client)
def dispatch(self, request, client):
......
......@@ -16,7 +16,11 @@ except:
coco = Coco()
coco.config.from_object(conf)
print(coco.root_path)
# Todo:
# 0. argparser
# 1. register application user
# 2. backup record file
# 3. xxx
if __name__ == '__main__':
coco.run_forever()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment