forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-telnet.py
40 lines (34 loc) · 1019 Bytes
/
python-telnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import socket, threading
import sys
HOST = ''
PORT = 10002
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)
clients = [] #list of clients connected
lock = threading.Lock()
class chatServer(threading.Thread):
def __init__(self, (socket,address)):
threading.Thread.__init__(self)
self.socket = socket
self.address= address
def run(self):
lock.acquire()
clients.append(self)
lock.release()
#print '%s:%s connected.' % self.address
print "connected"
while True:
data = self.socket.recv(1024)
if not data:
break
sys.stdout.write(data)
self.socket.close()
#print '%s:%s disconnected.' % self.address
print "disconnect"
lock.acquire()
clients.remove(self)
lock.release()
while True: # wait for socket to connect
# send socket to chatserver and start monitoring
chatServer(s.accept()).start()