Created
October 13, 2014 17:47
-
-
Save 1995eaton/97db2d6e62bb58205778 to your computer and use it in GitHub Desktop.
Simple Python Socket Messenger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
from threading import Thread | |
import socket | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(('localhost', 9999,)) | |
class ReplyHandler(Thread): | |
def __init__(self): | |
Thread.__init__(self) | |
def run(self): | |
while True: | |
reply = sock.recv(1024) | |
print(reply) | |
thread = ReplyHandler() | |
thread.daemon = True | |
thread.start() | |
while True: | |
message = raw_input('') | |
sock.sendall(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
from threading import Thread | |
import socket | |
sock, peers = None, [] | |
class IMS(object): | |
MAX_CONNECTIONS = 5 | |
def __init__(self): | |
self.setup() | |
for i in range(IMS.MAX_CONNECTIONS): | |
thread = IMS.Connection() | |
thread.daemon = True | |
thread.start() | |
def setup(self): | |
global sock | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) | |
sock.bind(('localhost', 9999,)) | |
sock.listen(10) | |
def send_message(self, message): | |
for peer in peers: | |
peer.sendall(message) | |
class Connection(Thread): | |
def __init__(self): | |
Thread.__init__(self) | |
def run(self): | |
peer, addr = sock.accept() | |
peers.append(peer) | |
while True: | |
message = peer.recv(1024) | |
print(message) | |
for other in peers: | |
if peer != other: | |
other.sendall(message) | |
ims = IMS() | |
try: | |
while 1: | |
message = raw_input('') | |
ims.send_message(message) | |
except KeyboardInterrupt: | |
sock.close() | |
for peer in peers: | |
peer.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
7yui