Python 带有活动客户端连接的while循环中的打印输出问题

Python 带有活动客户端连接的while循环中的打印输出问题,python,client-server,Python,Client Server,我有一个python服务器/客户机,我正在尝试将数据(简单字符串)从客户机发送到服务器,这很好。此外,我总是打印出从客户端发送到控制台的数据以及从服务器收到的确认消息,请参见下面的打印输出: Sent: 29/08/2020 11:50:56 CPU=56.0C GPU=63.5C Received: OK 192.168.1.156 wrote: b'29/08/2020 11:50:56 CPU=56.0C GPU=63.5C' 客户端打印输出: Sent: 29/08/

我有一个python服务器/客户机,我正在尝试将数据(简单字符串)从客户机发送到服务器,这很好。此外,我总是打印出从客户端发送到控制台的数据以及从服务器收到的确认消息,请参见下面的打印输出:

Sent:     29/08/2020 11:50:56 CPU=56.0C GPU=63.5C
Received: OK
192.168.1.156 wrote:
b'29/08/2020 11:50:56 CPU=56.0C GPU=63.5C'
客户端打印输出:

Sent:     29/08/2020 11:50:56 CPU=56.0C GPU=63.5C
Received: OK
192.168.1.156 wrote:
b'29/08/2020 11:50:56 CPU=56.0C GPU=63.5C'
服务器打印输出:

Sent:     29/08/2020 11:50:56 CPU=56.0C GPU=63.5C
Received: OK
192.168.1.156 wrote:
b'29/08/2020 11:50:56 CPU=56.0C GPU=63.5C'
非常简单的客户机/服务器通信。 现在我想让客户端保持一个while循环,每300秒发送一次数据。但它停止打印到客户端站点上的控制台。通信已启动并运行,服务器正确获取数据

服务器代码:

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the OK
        self.request.sendall(bytes("OK\n", "utf-8"))

if __name__ == "__main__":
    HOST, PORT = "192.168.1.140", 9999

    # Create the server, binding to localhost on port 9999
    with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
        # Activate the server; this will keep running until you
        # interrupt the program with Ctrl-C
        server.serve_forever()
客户端代码:

import socket
import sys
import time

from contextlib import contextmanager
from datetime import datetime

@contextmanager
def socketcontext(*args, **kw):
    s = socket.socket(*args, **kw)
    try:
        yield s
    finally:
        s.close()

host, port = "192.168.1.140", 9999

# datetime object containing current date and time
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
data = dt_string + " CPU=56.0C GPU=63.5C"

while True:
    # datetime object containing current date and time
    now = datetime.now()
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    data = dt_string + " CPU=56.0C GPU=63.5C"

    # Create a socket (SOCK_STREAM means a TCP socket)
    with socketcontext(socket.AF_INET, socket.SOCK_STREAM) as sock:
        try:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.settimeout(5)
            # Connect to server and send data
            sock.connect((host, port))
            sock.sendall(bytes(data + "\n", "utf-8"))
        
            # Receive data from the server and shut down
            received = str(sock.recv(1024), "utf-8")
        
            # Print communication data
            print("Sent:     {}".format(data))
            print("Received: {}".format(received))
        
        except Exception as err:
            print("TCP connection to {}:{} failed with error {}.".format(host, port, err))
    time.sleep(300)
运行上述客户机代码将发送数据并从服务器接收OK,但控制台上不会显示这些变量data&received的打印输出。 在显示打印输出时删除,但客户端发送一次并结束,我需要再次执行它,或者我必须通过bash while循环命令作为一行程序执行客户端代码:

bash终端:

while true; do client.py; sleep 300; done
我不喜欢

我做错了什么