Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 44 lines (38 sloc) 1.28 KB
#!/usr/bin/env python3
# python 3.3 or ++
import configparser
import sqlalchemy
from sqlalchemy import create_engine
from socket import *
configfile='/opt/echoserver/config/echoserver.conf'
config = configparser.ConfigParser()
config.read(configfile)
def db_display(config):
eng = create_engine('mysql+pymysql://%s:%s@127.0.0.1:3306' % (config['config']['username'], config['config']['password']),
pool_recycle=3600)
insp = sqlalchemy.inspect(eng)
db_list = insp.get_schema_names()
return db_list
def tcp_server(address, handler):
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(5)
while True:
client, addr = sock.accept()
handler(client, addr)
def echo_handler(client,address):
print('Connection from:', address)
client.send(b'Welcome to echoserver. Here is a list of databases. Anything you type will be echoed back to you. Have fun!\n')
client.send(bytes(msg, 'utf-8'))
while True:
data = client.recv(1000)
if not data:
break
client.send(data)
print('Connection closed')
client.close()
if __name__ == '__main__':
# pull list of database connections, stuff them in a string
msg = '\n'.join(db_display(config))
tcp_server(('',25000), echo_handler)
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.