Tak potřebuji nějakou realtime službu ala chat server našel jsem všechny pouze pod nodejs (asi je to dnes moderní kdekoli používat nodejs 🙂 ), ale já se snažím naučit pořádně python zde jeden ukázkový socket telnet server , který vychází z projektu miniboa, trochu jsem se přiučil také u projektu python-chatroom ten je zajímavý tím , že používá základní knihovny socket no a zde malá ukázka
__author__ = 'ucitel'
from miniboa import TelnetServer
import time
IDLE_TIMEOUT = 300
CLIENT_LIST = []
SERVER_RUN = True
time_down = 9999999999999999
def on_connect(client):
"""
Sample on_connect function.
Handles new connections.
"""
print "++ Opened connection to %s" % client.addrport()
broadcast('%s joins the conversation.\n' % client.addrport() )
client.send("Welcome to the Chat Server, %s.\n" % client.addrport() )
client.username=''
CLIENT_LIST.append(client)
#client.send('Moje jmeno ' + CLIENT_LIST['name'])
def on_disconnect(client):
"""
Sample on_disconnect function.
Handles lost connections.
"""
print "-- Lost connection to %s" % client.addrport()
CLIENT_LIST.remove(client)
broadcast('%s leaves the conversation.\n' % client.addrport() )
def kick_idle():
"""
Looks for idle clients and disconnects them by setting active to False.
"""
## Who hasn't been typing?
for client in CLIENT_LIST:
if client.idle() > IDLE_TIMEOUT:
print('-- Kicking idle lobby client from %s' % client.addrport())
client.active = False
def process_clients():
"""
Check each client, if client.cmd_ready == True then there is a line of
input available via client.get_command().
"""
for client in CLIENT_LIST:
if client.active and client.cmd_ready:
## If the client sends input echo it to the chat room
chat(client)
def broadcast(msg):
"""
Send msg to every client.
"""
for client in CLIENT_LIST:
client.send(msg)
def chat(client):
"""
Echo whatever client types to everyone.
"""
global SERVER_RUN
global time_down
msg = client.get_command()
#print '%s says, "%s"' % (client.username, msg)
radek = msg.lower()
cmd = radek.split(' ')[0]
## bye = disconnect
if cmd == 'time':
local_time = time.localtime()
client.send('The actual time is %i:%i:%i\n' % (local_time.tm_hour, local_time.tm_min, local_time.tm_sec))
client.active = True
elif cmd == 'up':
local_time = time.time()
up_time = local_time - start_time
client.send('The actual uptime is %f s\n' % (up_time))
client.active = True
elif cmd == 'down':
time_down = time.time() + float(radek.split(' ')[1])
client.send('Server is down as %s s\n' % (radek.split(' ')[1]))
client.active = True
elif cmd == 'bye':
client.active = False
## shutdown == stop the server
elif cmd == 'shutdown':
SERVER_RUN = False
elif cmd == 'user':
username=radek.split(' ')[1]
client.username=username
print 'User %s log in system' % (client.username)
client.active = True
elif cmd=='send':
for guest in CLIENT_LIST:
if radek.split(' ')[1] == guest.username:
guest.send('%s says, %s\n' % (client.username, radek.split(' ')[2]))
print 'User %s says "%s" to user %s' % (guest.username,radek.split(' ')[2],client.username)
else:
client.send('Sorry user %s not exist\n' % radek.split(' ')[1])
print 'User %s not exist' % radek.split(' ')[1]
client.active=True
elif cmd== 'all':
for guest in CLIENT_LIST:
if guest != client:
guest.send('%s says, %s\n' % (client.username, radek.split(' ')[1]))
else:
guest.send('You say, %s\n' % msg)
client.active=True
elif cmd == 'users':
client.send('Uzivatele ve skupine\n')
client.send('--------------------\n')
for guest in CLIENT_LIST:
client.send('%s\n' % (guest.username))
client.active=True
else:
client.active=True
#------------------------------------------------------------------------------
# Main
#------------------------------------------------------------------------------
if __name__ == '__main__':
## Simple chat server to demonstrate connection handling via the
## async and telnet modules.
## Create a telnet server with a port, address,
## a function to call with new connections
## and one to call with lost connections.
telnet_server = TelnetServer(
port=7777,
address='',
on_connect=on_connect,
on_disconnect=on_disconnect,
timeout = .05
)
start_time=time.time()
print(">> Listening for connections on port %d. CTRL-C to break."
% telnet_server.port)
## Server Loop
while SERVER_RUN:
if time.time() > time_down :
SERVER_RUN = False
telnet_server.poll() ## Send, Recv, and look for new connections
kick_idle() ## Check for idle clients
process_clients() ## Check for client input
print(">> Server shutdown.")