python操作clickhouse,报错Code: 102. Unexpected packet from server None

今天用python操作clickhouse时,报错

提示8123端口不可用,但是8123端口在dbeaver上用的很正常,连接的配置项也都是一样的。

最后终于弄清楚了

是python里的clickhouse_driver用的tcp端口9000。dbeaver用的jdbc连接走的http所以可以用8123端口。

把python中连接的端口换成9000。Code: 102.问题完美解决。

import socket import os import struct import select import time # ICMP协议号 ICMP_PROTO = socket.getprotobyname('icmp') # 创建原始套接字 try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, ICMP_PROTO) except PermissionError: print("错误:需要管理员权限运行此程序。") exit(1) # 设置超时 sock.settimeout(1) def checksum(source_string): sum = 0 countTo = (len(source_string) // 2) * 2 count = 0 while count < countTo: thisVal = source_string[count + 1] * 256 + source_string[count] sum = sum + thisVal sum = sum & 0xffffffff count = count + 2 if countTo < len(source_string): sum = sum + source_string[len(source_string) - 1] sum = sum & 0xffffffff sum = (sum >> 16) + (sum & 0xffff) sum = sum + (sum >> 16) answer = ~sum answer = answer & 0xffff answer = answer >> 8 | (answer << 8 & 0xff00) return answer def create_packet(id): # ICMP头:类型(8), 代码(0), 校验和(0), ID, 序列号(1) header = struct.pack('bbHHh', 8, 0, 0, id, 1) data = b'abcdefghijklmnopqrstuvwabcdefghi' my_checksum = checksum(header + data) header = struct.pack('bbHHh', 8, 0, socket.htons(my_checksum), id, 1) return header + data def send_ping(dest_addr): packet_id = os.getpid() & 0xFFFF packet = create_packet(packet_id) sock.sendto(packet, (dest_addr, 1)) send_time = time.time() return packet_id, send_time def receive_ping(packet_id, timeout): while True: ready = select.select([sock], [], [], timeout) if ready[0] == []: return None recv_time = time.time() recv_packet, addr = sock.recvfrom(1024) icmp_header = recv_packet[20:28] type, code, checksum, p_id, sequence = struct.unpack('bbHHh', icmp_header) if p_id == packet_id: return recv_time timeout = timeout - (time.time() - recv_time) if timeout <= 0: return None 请为我讲解一下这段代码
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值