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

2024-03-28 15:21:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个客户端,每10秒刷新一次服务器。当客户端签入服务器时。我希望服务器将标签文本值发送回客户端,以应用于客户端标签。你知道吗

客户端发送一个串行密钥,一旦服务器验证了它。服务器向客户端发回一条消息。我有一个itemPrice作为一个标签文本,在连接中断之前发送给同一个操作。我不知道如何在客户端检索itemPrice。有没有一种方法可以标记要用于客户机变量的值?你知道吗

服务器将保存客户端标签的文本。如何将此文本作为客户端上的变量发送?你知道吗

你知道吗客户端.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()

你知道吗服务器.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()

你知道吗价格.py你知道吗

itemPrice1 = b'0.01'

你知道吗数据.py你知道吗

serialNumber = b'001'

Tags: the文本import服务器客户端dataserveraddress
1条回答
网友
1楼 · 发布于 2024-03-28 15:21:23

对于服务器。我去掉了变量,直接把价格从价格.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

为了客户。我将变量直接放入定义中。你知道吗

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)

我必须定义袜子接收()使字符计数彼此不重叠。例如。你知道吗

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

相关问题 更多 >