Doing/Python
Socket_Chatting
YongArtist
2016. 12. 22. 07:56
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 | __author__="Introfor" # -*-coding: utf-8 -*- # server.py import socket import thread HOST = 'localhost' PORT = 50000 s = socket.socket() s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr data = '' def msg(): while 1: data = conn.recv(1024) if data: print data.lower() thread.start_new_thread(msg, ()) while 1: send_data = raw_input() if send_data[:4] in ['quit', 'Quit', 'QUIT']: conn.send('Server Quit') thread.exit() conn.close() conn.send('Server : ' + send_data) | cs |
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 | __author__="Introfor" # -*-coding: utf-8 -*- # client.py import socket import thread HOST = 'localhost' PORT = 50000 s = socket.socket() s.connect((HOST, PORT)) data = '' def msg(): while 1: data = s.recv(1024) if data: print data.lower() thread.start_new_thread(msg, ()) while 1: send_data = raw_input() if send_data in ['quit', 'Quit', 'QUIT']: s.send('Client Quit') thread.exit() s.close() s.send('Client : ' + send_data) | cs |