简体   繁体   English

python socket客户端如何在不被阻塞的情况下接收

[英]How can python socket client receive without being blocked

A simple demo of socket programming in python: 一个简单的python套接字编程演示:

server.py server.py

import socket

host = '127.0.0.1'
port = 8000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)

conn, addr = s.accept()

while True:
    data = conn.recv(1024)
    print 'Received:', data
    if not data:
        break
    conn.sendall(data)
    print 'Sent:', data

conn.close()

client.py client.py

import socket

host = '127.0.0.1'
port = 8000

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

s.connect((host, port))
s.sendall('Hel')
s.sendall('lo world!')
print 'Received:', s.recv(1024)

s.close()

Now code work well. 现在代码运行良好。 However, the client may not know if server will always send back every time. 但是,客户端可能不知道服务器是否每次都会始终发送回。 I tried symmetric code of while-loop in server.py 我在server.py中尝试了while循环的对称代码

client_2.py client_2.py

import socket

host = '127.0.0.1'
port = 8000

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

s.connect((host, port))
s.sendall('Hel')
s.sendall('lo world!')
while True:
    data = s.recv(1024)
    if not data:
        break
print 'Received:', data

s.close()

This code will block at 此代码将在

data = s.recv(1024)

But in server.py, if no data received, it will be blank string, and break from while-loop 但是在server.py中,如果未接收到任何数据,它将为空字符串,并从while循环中断

Why it does not work for client? 为什么它对客户不起作用? How can I do for same functionality without using timeout? 如何在不使用超时的情况下实现相同的功能?

You can set a socket to non-blocking operation via socket.setblocking(false) , which is equivalent to socket.settimeout(0) . 您可以通过socket.setblocking(false)将套接字设置为非阻塞操作,这等效于socket.settimeout(0) Solving this "without using timeout" is impossible. 解决“不使用超时”是不可能的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在不知道或无法选择消息长度的情况下从套接字客户端接收(接收)不同大小的字节吗? - Can I receive (recv) bytes of different sizes from socket client without knowing or being able to choose how long the message will be? 如何在 Python3 的套接字聊天室中发送和接收,而不会在打字时中断客户端? - How can I send and recieve in a socket chat room in Python3 without the client being interrupted while typing? 如何使用 Python(套接字)从客户端向客户端发送和接收消息? - How to send and receive message from client to client with Python (socket)? python socket.io客户端无法接收广播消息 - python socket.io client can't receive broadcasting messages Python 套接字客户端:发送数据和接收结果 - Python socket client: send data and receive result 如何使用 Python 和 socket 发送和接收? - How to send and receive with Python and socket? 如何在没有客户端首先请求 python 和 flask 的情况下通过 socket-io 发送数据 - How can I send data though socket-io without the client requesting first with python and flask python UDP socket客户端需要发送两次才能让服务器接收到包 - python UDP socket client need to send twice so that the server can receive the package 我的Python套接字服务器只能从客户端接收一条消息 - My Python socket server can only receive one message from the client 套接字可以接收无限数据流python吗? - Can socket receive infinite data stream python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM