简体   繁体   English

如何在 Python 中从服务器向客户端发送连续数据?

[英]How to send continous data from server to client in Python?

I am building a server to send data to the client in Python.我正在构建一个服务器以将数据发送到 Python 中的客户端。 I would like to continuously send the time until the client closes the connection.我想不断发送时间,直到客户端关闭连接。 So far, I have done:到目前为止,我已经完成了:

For the server:对于服务器:

import socket
from datetime import datetime

# take the server name and port name
host = 'local host'
port = 5001

# create a socket at server side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)

# bind the socket with server
# and port number
s.bind(('', port))

# allow maximum 1 connection to
# the socket
s.listen(1)

# wait till a client accept
# connection
c, addr = s.accept()

# display client address
print("CONNECTION FROM:", str(addr))

dateTimeObj = str(datetime.now())
print(dateTimeObj)

c.send(dateTimeObj.encode())

# disconnect the server
c.close()

For the client:对于客户:

import socket

# take the server name and port name

host = 'local host'
port = 5001

# create a socket at client side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)

# connect it to server and port
# number on local computer.
s.connect(('127.0.0.1', port))

# receive message string from
# server, at a time 1024 B
msg = s.recv(1024)

# repeat as long as message
# string are not empty
while msg:
    print('Received date :' + msg.decode())
    msg = s.recv(1024)

# disconnect the client
s.close()

How can I modify the server to continously send the current date?如何修改服务器以连续发送当前日期? At the moment, the server is just sending one date and closing the connection.目前,服务器只是发送一个日期并关闭连接。

you need to use While True loop.您需要使用While True循环。

import socket
from datetime import datetime

# take the server name and port name
host = 'local host'
port = 5001

# create a socket at server side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)

# bind the socket with server
# and port number
s.bind(('', port))

# allow maximum 1 connection to
# the socket
s.listen(1)

# wait till a client accept
# connection
while True:
    c, addr = s.accept()

    # display client address
    print("CONNECTION FROM:", str(addr))

    dateTimeObj = str(datetime.now())
    print(dateTimeObj)

    c.send(dateTimeObj.encode())

    # disconnect the server
    c.close()

client:客户:

import socket

# take the server name and port name

host = 'local host'
port = 5001

# create a socket at client side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)

# connect it to server and port
# number on local computer.
s.connect(('127.0.0.1', port))

# receive message string from
# server, at a time 1024 B
while True:
    msg = s.recv(1024)

    # repeat as long as message
    # string are not empty
    while msg:
        print('Received date :' + msg.decode())
        msg = s.recv(1024)

# disconnect the client
s.close()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM