Skip to content

Instantly share code, notes, and snippets.

@cyraxjoe
Created June 30, 2014 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyraxjoe/73eae99408f091c8b29d to your computer and use it in GitHub Desktop.
Save cyraxjoe/73eae99408f091c8b29d to your computer and use it in GitHub Desktop.
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