<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://wiki.twig.es/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.twig.es/index.php?action=history&amp;feed=atom&amp;title=Echo_server</id>
		<title>Echo server - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.twig.es/index.php?action=history&amp;feed=atom&amp;title=Echo_server"/>
		<link rel="alternate" type="text/html" href="https://wiki.twig.es/index.php?title=Echo_server&amp;action=history"/>
		<updated>2026-05-06T15:51:29Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.0</generator>

	<entry>
		<id>https://wiki.twig.es/index.php?title=Echo_server&amp;diff=4890&amp;oldid=prev</id>
		<title>George2: Created page with &quot;&lt;source lang=&quot;python&quot;&gt; #Example app - TCP Echo Server #Make a TCP server in a process that handles multiple clients #Echos back the data the client sent  #Imports import loggi...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.twig.es/index.php?title=Echo_server&amp;diff=4890&amp;oldid=prev"/>
				<updated>2022-01-15T11:25:30Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt; #Example app - TCP Echo Server #Make a TCP server in a process that handles multiple clients #Echos back the data the client sent  #Imports import loggi...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#Example app - TCP Echo Server&lt;br /&gt;
#Make a TCP server in a process that handles multiple clients&lt;br /&gt;
#Echos back the data the client sent&lt;br /&gt;
&lt;br /&gt;
#Imports&lt;br /&gt;
import logging&lt;br /&gt;
import multiprocessing&lt;br /&gt;
import socket&lt;br /&gt;
import select&lt;br /&gt;
&lt;br /&gt;
logging.basicConfig(format='%(levelname)s - %(asctime)s: %(message)s',datefmt='%H:%M:%S', level=logging.DEBUG)&lt;br /&gt;
&lt;br /&gt;
#Server&lt;br /&gt;
def chatserver(ip, port):&lt;br /&gt;
    server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)&lt;br /&gt;
    logging.info(f'Binding to {ip}:{port}')&lt;br /&gt;
    server.bind((ip,port))&lt;br /&gt;
    server.setblocking(False)&lt;br /&gt;
    server.listen(100)&lt;br /&gt;
    logging.info(f'Listening on {ip}:{port}')&lt;br /&gt;
&lt;br /&gt;
    readers = [server]&lt;br /&gt;
&lt;br /&gt;
    while True:&lt;br /&gt;
        readable, writable, errored = select.select(readers,[],[],0.5)&lt;br /&gt;
&lt;br /&gt;
        for s in readable:&lt;br /&gt;
            try:&lt;br /&gt;
                if s == server:&lt;br /&gt;
                    client, address = s.accept()&lt;br /&gt;
                    client.setblocking(False)&lt;br /&gt;
                    readers.append(client)&lt;br /&gt;
                    logging.info(f'Connection: {address}')&lt;br /&gt;
                else:&lt;br /&gt;
                    data = s.recv(1024)&lt;br /&gt;
                    if data:&lt;br /&gt;
                        logging.info(f'Echo: {data}')&lt;br /&gt;
                        s.send(data)&lt;br /&gt;
                    else:&lt;br /&gt;
                        logging.info(f'Remove: {s}')&lt;br /&gt;
                        s.close()&lt;br /&gt;
                        readers.remove(s)&lt;br /&gt;
&lt;br /&gt;
            except Exception as ex:&lt;br /&gt;
                logging.warning(ex.args)&lt;br /&gt;
            finally:&lt;br /&gt;
                pass&lt;br /&gt;
&lt;br /&gt;
#Main&lt;br /&gt;
def main():&lt;br /&gt;
    svr = multiprocessing.Process(target=chatserver,args=['localhost',2067],daemon=True,name='Server')&lt;br /&gt;
&lt;br /&gt;
    while True:&lt;br /&gt;
        command = input('Enter a command (start, stop)')&lt;br /&gt;
        if command == 'start':&lt;br /&gt;
            logging.info('Starting the server')&lt;br /&gt;
            svr.start()&lt;br /&gt;
        if command == 'stop':&lt;br /&gt;
            logging.info('Stopping the server')&lt;br /&gt;
            svr.terminate()&lt;br /&gt;
            svr.join()&lt;br /&gt;
            svr.close()&lt;br /&gt;
            logging.info('Server stopped')&lt;br /&gt;
&lt;br /&gt;
            break&lt;br /&gt;
    logging.info('Application finished')&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/python&amp;gt;&lt;/div&gt;</summary>
		<author><name>George2</name></author>	</entry>

	</feed>