Skip to content

Instantly share code, notes, and snippets.

@aduartem
Last active August 29, 2016 03:35
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 aduartem/33fef96fe93556c5ea2e to your computer and use it in GitHub Desktop.
Save aduartem/33fef96fe93556c5ea2e to your computer and use it in GitHub Desktop.
Python - Socket Examples
#!/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()
#!/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