implement a some server

pull/530/head
ibuler 8 years ago
parent e020aaa368
commit db2d00f828

@ -57,84 +57,87 @@ class Server(paramiko.ServerInterface):
return True
def check_channel_pty_request(self, channel, term, width, height, pixelwidth,
pixelheight, modes):
pixelheight, modes):
return True
def handle_ssh_request(client, addr):
print('Got a connection!')
class SSHServer:
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
try:
t = paramiko.Transport(client, gss_kex=False)
t.set_gss_host(socket.getfqdn(""))
try:
t.load_server_moduli()
except:
print('(Failed to load moduli -- gex will be unsupported.)')
raise
t.add_server_key(host_key)
server = Server()
try:
t.start_server(server=server)
except paramiko.SSHException:
print('*** SSH negotiation failed.')
return
@staticmethod
def handle_ssh_request(client, addr):
print('Got a connection!')
while True:
# wait for auth
chan = t.accept(20)
if chan is None:
print('*** No channel.')
return
print('Authenticated!')
server.event.wait(10)
if not server.event.is_set():
print('*** Client never asked for a shell.')
return
chan.send('\r\n\r\nWelcome to my dorky little BBS!\r\n\r\n')
chan.send('We are on fire all the time! Hooray! Candy corn for everyone!\r\n')
chan.send('Happy birthday to Robot Dave!\r\n\r\n')
chan.send('Username: ')
f = chan.makefile('rU')
username = f.readline().strip('\r\n')
chan.send('\r\nI don\'t like you, ' + username + '.\r\n')
chan.close()
except Exception as e:
print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e))
traceback.print_exc()
try:
t.close()
except:
pass
sys.exit(1)
t = paramiko.Transport(client, gss_kex=False)
t.set_gss_host(socket.getfqdn(""))
try:
t.load_server_moduli()
except:
print('(Failed to load moduli -- gex will be unsupported.)')
raise
t.add_server_key(host_key)
server = Server()
server.add_prompt(">>")
try:
t.start_server(server=server)
except paramiko.SSHException:
print('*** SSH negotiation failed.')
return
while True:
# wait for auth
chan = t.accept(20)
if chan is None:
print('*** No channel.')
return
print('Authenticated!')
server.event.wait(10)
if not server.event.is_set():
print('*** Client never asked for a shell.')
return
chan.send('\r\n\r\nWelcome to my dorky little BBS!\r\n\r\n')
chan.send('We are on fire all the time! Hooray! Candy corn for everyone!\r\n')
chan.send('Happy birthday to Robot Dave!\r\n\r\n')
chan.send('Username: ')
f = chan.makefile('rU')
username = f.readline().strip('\r\n')
chan.send('\r\nI don\'t like you, ' + username + '.\r\n')
chan.close()
except Exception as e:
print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e))
traceback.print_exc()
try:
t.close()
except:
pass
sys.exit(1)
def listen(self):
self.sock.listen(5)
while True:
try:
client, addr = self.sock.accept()
print('Listening for connection ...')
threading.Thread(target=self.handle_ssh_request, args=(client, addr)).start()
except Exception as e:
print('*** Bind failed: ' + str(e))
traceback.print_exc()
sys.exit(1)
def run_server():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 2200))
except Exception as e:
print('*** Bind failed: ' + str(e))
traceback.print_exc()
sys.exit(1)
if __name__ == '__main__':
server = SSHServer('', 2200)
try:
sock.listen(100)
print('Listening for connection ...')
client, addr = sock.accept()
t = threading.Thread(target=handle_ssh_request, args=(client, addr))
t.start()
except Exception as e:
print('*** Listen/accept failed: ' + str(e))
traceback.print_exc()
server.listen()
except KeyboardInterrupt:
sys.exit(1)
if __name__ == '__main__':
run_server()

@ -0,0 +1,44 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import socket
import sys
import threading
class ThreadSocket:
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(5)
while True:
client, address = self.sock.accept()
client.settimeout(60)
threading.Thread(target=self.handle_client_request, args=(client, address)).start()
def handle_client_request(self, client, address):
print("Get client: %s" % str(address))
while True:
try:
data = client.recv(1024)
print("sleep : %s" % str(address))
if data:
client.send(data)
else:
raise IndexError('Client has disconnected')
except:
client.close()
if __name__ == '__main__':
server = ThreadSocket('', 9000)
try:
server.listen()
except KeyboardInterrupt:
sys.exit(1)
Loading…
Cancel
Save