Commit 6a623a6d authored by ibuler's avatar ibuler

[Update] Change select to selector

parent 6f072d57
...@@ -37,7 +37,7 @@ class Coco: ...@@ -37,7 +37,7 @@ class Coco:
def __init__(self, name=None, root_path=None): def __init__(self, name=None, root_path=None):
self.config = self.config_class(BASE_DIR, defaults=self.default_config) self.config = self.config_class(BASE_DIR, defaults=self.default_config)
self.sessions = [] self.sessions = []
self.connections = [] self.clients = []
self.ws = None self.ws = None
self.root_path = root_path self.root_path = root_path
self.name = name self.name = name
...@@ -96,6 +96,16 @@ class Coco: ...@@ -96,6 +96,16 @@ class Coco:
print("Grace shutdown the server") print("Grace shutdown the server")
self.sshd.shutdown() 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): def monitor_session(self):
pass pass
#coding: utf-8 # coding: utf-8
import socket import socket
import paramiko import paramiko
...@@ -34,7 +35,6 @@ class ProxyServer: ...@@ -34,7 +35,6 @@ class ProxyServer:
pass pass
def get_server_conn(self, asset, system_user): def get_server_conn(self, asset, system_user):
self.ssh = ssh = paramiko.SSHClient() self.ssh = ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try: try:
......
...@@ -47,6 +47,8 @@ class InteractiveServer: ...@@ -47,6 +47,8 @@ class InteractiveServer:
r, w, x = select.select([self.client], [], []) r, w, x = select.select([self.client], [], [])
if self.client in r: if self.client in r:
data = self.client.recv(10) data = self.client.recv(10)
if len(data) == 0:
self.app.remove_client(self.client)
# Client input backspace # Client input backspace
if data in char.BACKSPACE_CHAR: if data in char.BACKSPACE_CHAR:
# If input words less than 0, should send 'BELL' # If input words less than 0, should send 'BELL'
...@@ -80,6 +82,13 @@ class InteractiveServer: ...@@ -80,6 +82,13 @@ class InteractiveServer:
input_data.append(data) input_data.append(data)
def dispatch(self, opt): 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) 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") system_user = SystemUser(id=2, username="web", password="redhat123", name="web")
self.connect(asset, system_user) self.connect(asset, system_user)
......
...@@ -49,7 +49,7 @@ class SystemUser(Decoder): ...@@ -49,7 +49,7 @@ class SystemUser(Decoder):
name = "" name = ""
username = "" username = ""
password = "" password = ""
private_key = "" private_key = None
def __str__(self): def __str__(self):
return self.name return self.name
......
...@@ -51,7 +51,7 @@ class Session: ...@@ -51,7 +51,7 @@ class Session:
while self.running: while self.running:
try: try:
r, w, x = select.select([self.client + self.server] r, w, x = select.select([self.client + self.server]
+ self.watchers + self.sharers, [], []) + self.sharers, [], [])
for sock in r: for sock in r:
if sock == self.server: if sock == self.server:
...@@ -67,8 +67,6 @@ class Session: ...@@ -67,8 +67,6 @@ class Session:
watcher.send("%s close the session" % self.client) watcher.send("%s close the session" % self.client)
self.close() self.close()
self.server.send(data) 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: elif sock in self.sharers:
data = sock.recv(BUF_SIZE) data = sock.recv(BUF_SIZE)
if len(data) == 0: if len(data) == 0:
......
...@@ -61,13 +61,11 @@ class SSHServer: ...@@ -61,13 +61,11 @@ class SSHServer:
try: try:
sock, addr = self.sock.accept() sock, addr = self.sock.accept()
logger.info("Get ssh request from %s: %s" % (addr[0], addr[1])) 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() sock.close()
logger.warning("Arrive max connection number %s, " logger.warning("Arrive max connection number %s, "
"reject new request %s:%s" % "reject new request %s:%s" %
(max_conn_num, addr[0], addr[1])) (max_conn_num, addr[0], addr[1]))
else:
self.app.connections.append((sock, addr))
thread = threading.Thread(target=self.handle, args=(sock, addr)) thread = threading.Thread(target=self.handle, args=(sock, addr))
thread.daemon = True thread.daemon = True
thread.start() thread.start()
...@@ -101,6 +99,7 @@ class SSHServer: ...@@ -101,6 +99,7 @@ class SSHServer:
sys.exit(2) sys.exit(2)
client = Client(chan, addr, request.user) client = Client(chan, addr, request.user)
self.app.add_client(client)
self.dispatch(request, client) self.dispatch(request, client)
def dispatch(self, request, client): def dispatch(self, request, client):
......
...@@ -16,7 +16,11 @@ except: ...@@ -16,7 +16,11 @@ except:
coco = Coco() coco = Coco()
coco.config.from_object(conf) 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__': if __name__ == '__main__':
coco.run_forever() 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