Skip to content

Instantly share code, notes, and snippets.

@yingshaoxo
Last active May 28, 2017 09:43
Show Gist options
  • Save yingshaoxo/7b5fe8f20186c64623c410972ac4f776 to your computer and use it in GitHub Desktop.
Save yingshaoxo/7b5fe8f20186c64623c410972ac4f776 to your computer and use it in GitHub Desktop.
asyncore TCP client
import asyncore
import threading
class MySocketClient(asyncore.dispatcher):
def __init__(self, server_address):
asyncore.dispatcher.__init__(self)
self.create_socket()
self.connect(server_address)
def handle_read(self):
data = self.recv(8192)
if data:
print(data)
client = MySocketClient(('127.0.0.1', 5920))
threading.Thread(target=asyncore.loop).start()
while True:
sending_text = ''
while sending_text == '':
sending_text = input('\nSay somethine: ')
client.send(sending_text.encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment