python socket 使用

服务端:(使用多线程,多个客户端可同时连接,这里是将kafka的队列信息实时返回到客户端)

import socket
import time
import threading
from kafka import KafkaConsumer, KafkaProducer, TopicPartition
from concurrent.futures import ThreadPoolExecutor
import uuid

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind(('127.0.0.1', 5678))
s.listen(5)
print('Waiting for connection...')


def tcplink(sock, addr):
    try:
        print('Accept new connection from %s:%s...' % addr)
        sock.send(b'Welcome!')
        cnt = 1
        consumer = KafkaConsumer(bootstrap_servers=['***.***.***.***:****'],
                                 group_id='randgroup_id_' + str(uuid.uuid1()),
                                 auto_offset_reset='latest', consumer_timeout_ms=600000)
        consumer.assign([
            TopicPartition(topic="test0324", partition=0)
        ])
        print('type>>>>>', type(consumer))
        for msg in consumer:
            info = msg.value
            print(info)
            # data = sock.recv(1024)
            time.sleep(1)
            # if not data or data.decode('utf-8') == 'exit':
            #     break
            # sock.send(('Hello, %s!' % data.decode('utf-8')).encode('utf-8'))
            sock.send(str(info).encode('utf-8'))
            cnt += 1
        print('长时间无数据,关闭线程,关闭socket连接: ', sock)
        sock.close()
    except Exception as e:
        print(e)
        sock.close()
        print('Connection from %s:%s closed.' % addr)


def main():
    task_list = []
    max_workers = 4  # 最大线程数
    pool = ThreadPoolExecutor(max_workers=max_workers, thread_name_prefix='Thread')
    while True:
        # 接受一个新连接:
        sock, addr = s.accept()
        for _i, _n in enumerate(task_list):
            if _n.done():
                task_list.pop(_i)
        print('>>>>', len(task_list))
        task_list.append(pool.submit(tcplink, sock, addr))


if __name__ == '__main__':
    main()

 

 客户端:

import socket


def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('127.0.0.01', 5678))
    while True:
        # input_ = input('请输入:')
        # input_ = input_.encode('utf-8')
        # s.send(input_)
        d = s.recv(1024)
        d = d.decode('utf-8')
        print('收到:', d)


if __name__ == '__main__':
    main()

 

posted @ 2022-03-24 16:46  牛郎  阅读(221)  评论(0编辑  收藏  举报