多线程socket客户端

1076阅读 0评论2012-10-10 niexining
分类:Python/Ruby


点击(此处)折叠或打开

  1. import socket
  2. from threading import Thread
  3. import sys

  4. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. port = input("port: ")
  6. sock.connect(('localhost', port))

  7. def reader():
  8.     while 1:
  9.         buf = sock.recv(1024)
  10.         if buf:
  11.             for b in buf: print hex(ord(b)),

  12. def writer():
  13.     while 1:
  14.         buf = raw_input()
  15.         sock.send(buf)

  16.         
  17. r=Thread(target=reader,args=())
  18. w=Thread(target=writer,args=())
  19. r.start()
  20. w.start()

上一篇:python socket 服务器和客户端
下一篇:crc16 python程序