#!/usr/bin/python #client from socket import * HOST = ‘127.0.0.1‘ PORT = 21567 BUFSIZ = 1024 ADDR = (HOST,PORT) tcpCliSock = socket(AF_INET, SOCK_STREAM) tcpCliSock.connect(ADDR) recv_s = tcpCliSock.recv(BUFSIZ) print recv_s try: while True: send_c = raw_input(‘clinet: ‘) tcpCliSock.send(‘client: %s‘ % (send_c)) recv_s = tcpCliSock.recv(BUFSIZ) print recv_s finally: tcpCliSock.close()
服务器:
#!/usr/bin/python from socket import * HOST = ‘‘ PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) tcpSerSock = socket(AF_INET, SOCK_STREAM) tcpSerSock.bind(ADDR) tcpSerSock.listen(1) try: while True: print ‘waiting for connection...‘ tcpCliSock, addr = tcpSerSock.accept() print ‘...connected from:‘, addr tcpCliSock.send(‘Please have a talk...‘) try: while True: recv_c = tcpCliSock.recv(BUFSIZ) print recv_c send_s = raw_input(‘server: ‘) tcpCliSock.send(‘server: %s‘ % (send_s)) finally: tcpCliSock.close() finally: tcpSerSock.close()
时间: 2024-10-01 04:59:45