Last active
August 29, 2016 03:35
-
-
Save aduartem/33fef96fe93556c5ea2e to your computer and use it in GitHub Desktop.
Python - Socket Examples
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/python | |
# Socket Client Example | |
import socket | |
HOST = 'localhost' # El host remoto | |
PORT = 9999 # El puerto del host remoto | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
while True: | |
message = raw_input("> ") | |
s.send(message) | |
if message == ":exit": | |
break | |
print "Chao" | |
s.close() |
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/python | |
# Socket Server Example | |
import socket | |
HOST = 'localhost' | |
PORT = 9999 | |
s = socket.socket() | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
conn, addr = s.accept() | |
while 1: | |
data = conn.recv(1024) | |
if data == ":exit": | |
break | |
print "Recibido:", data | |
conn.send(data) | |
print "Chao" | |
conn.close() | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment