• 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()
    
  • 相关阅读:
    我的第一次面试 —— 腾讯 AI安全 一面总结
    从零开始openGL——五、光线追踪
    从零开始openGL——四、纹理贴图与n次B样条曲线
    从零开始openGL——三、模型加载及鼠标交互实现
    从零开始openGL—— 二、 基本图形绘制
    NN入门,手把手教你用Numpy手撕NN(三)
    2020计算机保研经验分享
    TCP实战二(半连接队列、全连接队列)
    TCP实战一(三握四挥、流量控制)
    tcpdump抓包工具的基本使用
  • 原文地址:https://www.cnblogs.com/niulang/p/16050484.html
Copyright © 2020-2023  润新知