python server send event_使用select()的Python TCP Server

this is my first entry @stackoverflow;-))

I need a Python3 TCP server, which sends all inputs of the clients to the other participants. The server works, but unfortunately the message is only returned to the sending client. As soon as the second client sends something, it gets all entries.The entries are therefore available in the output queue.

Why are the entries not recognized by select ()?

Thx for the support.

`

import select

import socket

import sys

import queue

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

server.setblocking(0)

server_address = ('192.168.100.28', 8888)

print ("starting up on %s port %s" % server_address)

server.bind(server_address)

server.listen(5)

inputs = [ server ]

outputs = [ ]

message_queues = {}

while inputs:

print ("\nwaiting for the next event")

readable, writable, exceptional = select.select(inputs, outputs, inputs)

Handle inputs

for s in readable:

if s is server:

# A "readable" server socket is ready to accept a connection

connection, client_address = s.accept()

print ("new connection from", client_address)

connection.setblocking(0)

inputs.append(connection)

# Give the connection a queue for data we want to send

message_queues[connection] = queue.Queue()

else:

data = s.recv(1024)

if data:

# A readable client socket has data

print ('received "%s" from %s' % (data, s.getpeername()))

# Add output channel for response

#message_queues[s].put(data)

if s not in outputs:

outputs.append(s)

for aQueue in message_queues:

message_queues[aQueue].put(data)

# AQueue.send ("Test".encode())

else:

# Interpret empty result as closed connection

print ("closing", client_address, "after reading no data")

# Stop listening for input on the connection

if s in outputs:

outputs.remove(s)

inputs.remove(s)

s.close()

# Remove message queue

del message_queues[s]

Handle outputs

for s in writable:

try:

next_msg = message_queues[s].get_nowait()

except queue.Empty:

# No messages waiting so stop checking for writability.

print ("output queue for", s.getpeername(), "is empty")

outputs.remove(s)

else:

print ('sending "%s" to %s' % (next_msg, s.getpeername()))

s.send(next_msg)

Handle "exceptional conditions"

for s in exceptional:

print ("handling exceptional condition for", s.getpeername())

# Stop listening for input on the connection

inputs.remove(s)

if s in outputs:

outputs.remove(s)

s.close()

# Remove message queue

del message_queues[s]

`

解决方案

Your problem is that you only add a peer to the output list when you read something from it. You should instead do it when you write somthing to its message queue.

The receiving part should become:

...

data = s.recv(1024)

if data:

# A readable client socket has data

print ('received "%s" from %s' % (data, s.getpeername()))

# Add output channel for response

#message_queues[s].put(data)

# if s not in outputs:

# outputs.append(s)

for aQueue in message_queues:

message_queues[aQueue].put(data)

if aQueue not in outputs: # enqueue the msg

outputs.append(aQueue) # and ask select to warn when it can be sent

# AQueue.send ("Test".encode())

else:

...

According to your own requirements, you should only enqueue messages for other participants:

for aQueue in message_queues:

if aQueue != s: # only send to other participants

...

Finally, you often test existence of a socket in outputs. That let think that a set would be more appropriate than a list.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值