def handle(conn, addr): """ Handle each connected client. """ logger.info('connected to {0}'.format(addr)) time.sleep(1) # delay # read payload payload_len_buf = read_bytes(conn, PLEN_BUF_SIZE) payload_len = struct.unpack('<L', payload_len_buf)[0] payload_buf = read_bytes(conn, payload_len) # shorten url and send it back short_url = shorten(payload_buf) payload_len = struct.pack('<L', len(short_url)) conn.sendall(payload_len + short_url) conn.close()
def run(): # connect to server client = socket() client.connect(('', 9000)) # send payload payload = 'http://127.0.0.1:5000/{0}'.format(uuid.uuid4()) payload_len = struct.pack('<L', len(payload)) client.sendall(payload_len + payload) # read payload payload_len_buf = read_bytes(client, PLEN_BUF_SIZE) payload_len = struct.unpack('<L', payload_len_buf)[0] payload_buf = read_bytes(client, payload_len) client.close() return payload_buf