import socket import time def dumpHex(buf): print 'data length is %d.' % len(buf) for ch in buf: print '0x%02x' % ord(ch), print def getSocket(ip, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) address = (ip, port) num = 0 while True: try: num += 1 sock.connect(address) break except Exception, e: print 'connecting error, retry %d ...' % num time.sleep(1) continue return sock def main(): sock = getSocket('127.0.0.1', 8080) while True: sock.send('get') data = sock.recv(1024) if len(data) > 0: #0-closed from the other end. dumpHex(data) continue else: break sock.close() if __name__ == '__main__': main()