forked from markmbadham/pythoncourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echoserver.py
50 lines (43 loc) · 1.34 KB
/
echoserver.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
41
42
43
44
45
46
47
48
49
50
import socket
import logging as log
import traceback as tb
from threading import Thread
FORMAT = '%(asctime)s:%(levelname)s:%(thread)d:%(message)s'
log.basicConfig(format=FORMAT, filename='server.log', level=log.DEBUG)
class EchoServer(Thread):
ssock = socket.socket()
running = True
def __init__(self, sock=None):
Thread.__init__(self)
if sock:
self.sock = sock
self.start()
else:
log.info('starting')
self.setup()
def setup(self):
try:
ssock = EchoServer.ssock
ssock.bind(('127.0.0.1',10001))
log.debug('bound to port')
ssock.listen(5)
while (EchoServer.running):
(sock,address)=ssock.accept()
#log.debug('connection accepted from %s' % address)
log.debug(address)
new_server = EchoServer(sock)
except:
log.error(tb.format_exc())
finally:
ssock.close()
def run(self):
self.sock.send('Welcome\n')
while(True):
line = self.sock.recv(8192)
log.debug('recvd: %s' % line)
self.sock.send(line)
if line.strip() == 'quit': break
self.sock.close()
log.debug('connection closed')
if __name__ == '__main__':
EchoServer()