Created
June 30, 2014 20:53
-
-
Save cyraxjoe/73eae99408f091c8b29d to your computer and use it in GitHub Desktop.
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
import sys | |
import socket | |
def client(*bparams): | |
s = socket.socket() | |
s.connect(bparams) | |
return s | |
def server(*bparams): | |
s = socket.socket() | |
s.bind(bparams) | |
s.listen(0) | |
return s | |
def talk(host, port, *smalltalk): | |
with server(host, port) as s, client(host, port) as cl: | |
srvconn,_ = s.accept() | |
for a, b in smalltalk: | |
cl.send(a.encode()) | |
print("Client:\t {}".format(srvconn.recv(8096).decode())) | |
srvconn.send(b.encode()) | |
print("Server:\t {}".format(cl.recv(8096).decode())) | |
def main(): | |
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8181 # default port | |
dialogs = ( | |
('Hi!', 'Whatsup!'), | |
('Just the usual', 'Oh... ok..'), | |
('... (unconfortably staring)', '... Ok... talk to you later...'), | |
('Ok Bye!', 'Bye! (rushes away)') | |
) | |
talk('0.0.0.0', port, *dialogs) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment