简体   繁体   English

如何在python中从服务器更新客户端标签?

[英]How to update client labels from a server in python?

I have a client that refreshes with the server every 10 seconds. 我有一个客户端,每10秒钟刷新一次服务器。 When the client checks in with the server. 客户端向服务器签入时。 I would like the server to send label text values back to the client to be applied to client labels. 我希望服务器将标签文本值发送回客户端以应用于客户端标签。

The client sends a serial key and once the server verifies it. 客户端发送一个串行密钥,一旦服务器验证了它。 the server sends back a message to the client. 服务器将消息发送回客户端。 I have a itemPrice to be used as a label text being sent in that same action before the connection breaks. 我有一个itemPrice可以用作连接断开之前在同一操作中发送的标签文本。 I have no idea how to retrieve the itemPrice on the client side. 我不知道如何在客户端检索itemPrice。 Is there a way of tagging the value to be used for a client variable? 有没有一种方法标记用于客户端变量的值?

The server will be holding the text for client labels. 服务器将保留客户端标签的文本。 How can i send this text to be a variable on the client? 我如何发送此文本作为客户端上的变量?

Client.py 客户端

from tkinter import *
import socket


root = Tk()
# Variables
itemPrice1 = ''
itemPrice2 = ''


# SERVER CONNECTION
serialNumber = '001'

# Define Refresh Time
def Refresh():
    root.after(10000, Refresh)  # every 10 seconds...
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    sock.send(serialNumber.encode('UTF-8'))
    amount_received = 0
    data = sock.recv(16)
    amount_received += len(data)
    print('received "%s"' % data)

# Layout
root.config(background='black')
item1 = Label(root, text=itemPrice1)
item1.grid(row=0, column=0)
item1.config(background='grey', width=10)
closeButton = Button(root, text='Close', command=root.destroy)
closeButton.grid(row=1, column=0)


Refresh()
root.mainloop()

Server.py Server.py

import socket
import data
import price

# VARIABLES
itemPrice1 = price.itemPrice1

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)

        # Receive the data in small chunks and retransmit it
        while True:
            dataRec = connection.recv(16)
            print(client_address, dataRec)
            if dataRec == data.serialNumber:
                print('sending data back to the client')
                connection.send('Serial Valid'.encode('UTF-8'))
                connection.send(itemPrice1)

                break
            else:
                connection.send('Key Not Valid'.encode('UTF-8'))
                break

    finally:
        # Clean up the connection
        connection.close()

Price.py Price.py

itemPrice1 = b'0.01'

Data.py 数据

serialNumber = b'001'

For the server. 对于服务器。 I removed the variables and forward the prices directly from the price.py 我删除了变量并直接从price.py转发价格

            if dataRec == data.serialNumber:
                print('sending data back to the client')
                connection.send('Serial Valid'.encode('UTF-8'))
                connection.send(price.itemPrice1.encode('UTF-8'))  # SENDING PRICE TO CLIENT FOR LABEL
                connection.send(price.itemPrice2.encode('UTF-8'))
                break

For the client. 对于客户。 I placed the variables directly in the definition. 我将变量直接放在定义中。

def Refresh():
    root.after(10000, Refresh)  # every 10 seconds...
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    sock.send(serialNumber.encode('UTF-8'))
    amount_received = 0
    data = sock.recv(12)
    amount_received += len(data)
    print('received "%s"' % data)
    itemPrice1 = sock.recv(4)
    print('price', itemPrice1)
    itemPrice1Label.config(text=itemPrice1)
    itemPrice2 = sock.recv(4)
    print('price', itemPrice2)
    itemPrice2Label.config(text=itemPrice2)

Doing this I have to define the sock.recv() amount so the character count doesn't overlap with each other. 为此,我必须定义sock.recv()的数量,以便字符计数不会相互重叠。 Example being. 例子是。

item price 1 = 1.05
item price 2 = 0.10

itemPrice1 = sock.recv(7)
would print 1.050.1
vs
itemPrice1 = sock.recv(4)
would print 1.05

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

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