Skip to content

Instantly share code, notes, and snippets.

@o3bvv
Created June 16, 2013 15:01
Show Gist options
  • Save o3bvv/5792312 to your computer and use it in GitHub Desktop.
Save o3bvv/5792312 to your computer and use it in GitHub Desktop.
import socket
import select
import sys
from threading import Thread
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
sock.connect( ('192.168.1.36', 20001) )
def client_runner():
while True:
try:
rlist, wlist, elist = select.select([sock], [sock], [], 0)
except socket.error, e:
print "A socket error"
break
if [rlist, wlist, elist] != [ [], [], [] ]:
line = sock.recv(200).rstrip()
if line.endswith("\\n"):
line = line[:-2]
if line.endswith("quit"):
break
if line.startswith("\u0020"):
line = line[6:]
print line
t = Thread(target = client_runner)
t.daemon = True
t.start()
while True:
line = sys.stdin.readline()
if not line:
break
sock.send(line)
if line.rstrip() in ["quit", "exit"]:
break
sock.close()
if t.is_alive():
t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment