python 多线程队列套接字

#server端
import
socket import selectors import threading from threading import Thread import time import queue selector = selectors.DefaultSelector() socket_server = socket.socket() address_server = ('0.0.0.0',8080) socket_server.bind(address_server) socket_server.listen(1000) q = queue.Queue(5) class Producer(Thread): '''进队 初始化套接字 套接字入队 ''' def __init__(self, q, socket_server): super().__init__() self.q = q self.socket_server = socket_server def run(self): while True: connect,address = self.socket_server.accept() self.q.put(connect) class Conmuer(Thread): '''出队 套接字出队 ''' def __init__(self, q): super().__init__() self.q = q def run(self): while True: connect = self.q.get() print(type(connect)) massage = connect.recv(1024) if massage: connect.send(massage) else: connect.close() # self.q.task_done() producer = Producer(q, socket_server) conmuer = Conmuer(q) producer.start() conmuer.start() client端
import socket
import time


socket = socket.socket()
address = ('127.0.0.1',8080)
socket.connect(address)
massage_send = input('请输入')
massage_send = bytes(massage_send, encoding='utf-8')
socket.send(massage_send)
massage_rev = str(socket.recv(1024), encoding='utf-8')
print(massage_rev)
 
  

 

 
 

 

转载于:https://www.cnblogs.com/arrow-kejin/p/8993290.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值