python简易socket
1.sever
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/usr/bin/python # -*- coding:utf-8 -*- if __name__ == '__main__': import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 8011)) sock.listen(5) while True: connection,address = sock.accept() try: connection.settimeout(5) buf = connection.recv(1024) if buf == '1': connection.send('welcome to eqi.cc server!') else: mess = raw_input() connection.send(mess) except socket.timeout: print 'time out' connection.close() |
2.client
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/python #-*- coding: UTF-8 -*- if __name__ == '__main__': import socket while True: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', 8011)) # import time # time.sleep(2) sock.send('2') print sock.recv(1024) sock.close() |
https://github.com/rongtian/scoket