在python 3中发送和接收字节的好方法是什么?

时间:2017-03-18 19:55:50

标签: python networking tkinter

所以我正在为我的网络类中的MUD工作,我很少知道如何从客户端向服务器发送和接收以字节为单位的命令,游戏协议说所有消息都是小端和我一直在寻找方法来发送和接收正确字节的消息,有人能指出我正确的方向吗?

以下是protocal的链接:http://isoptera.lcsc.edu/~seth/cs435/lurk_2.0.html

这是我的代码到目前为止,函数在shell中打印消息是我想将代码发送到服务器的地方。

from tkinter import *
from sys import exit

userEntry = object

def button_func():
    print("Test")

def start_func():
    messg = (6).to_bytes(1, byteorder="little")
    print(messg)

def change_func():
    messg = (2).to_bytes(1, byteorder="little")
    print(messg)

def fight_func():
    messg = (3).to_bytes(1, byteorder="little")
    print(messg)

def pvp_func():
    messg = (4).to_bytes(1, byteorder="little")
    print(messg)

def loot_func():
    messg = (5).to_bytes(1, byteorder="little")
    print(messg)

def leave_func():
    messg = (12).to_bytes(1, byteorder="little")
    print(messg)

def submit_value():
    global userEntry
    print("Entered Value: %s" % (userEntry.get()))
    e1.delete(0, END)

class TestClient(Frame):
    def __init__(self, master):
        global userEntry
        Frame.__init__(self, master)
        self.pack()

        for n in range(3):
            self.grid_rowconfigure(n, weight=1)

        for n in range(8):
            self.grid_columnconfigure(n, weight=1)

        lb1 = Listbox(self, width=20,height=24)
        lb1.insert(1,"Players Connected")
        lb1.grid(row=0, column=0, columnspan=2, sticky='news')

        t1 = Text(self, width=60)
        t1.grid(row=0, column=3, columnspan=3)

        lb2 = Listbox(self, width=20,height=24)
        lb2.insert(1,"Connected rooms")
        lb2.grid(row=0, column=6, columnspan=2, sticky='news')

        la1 = Label(self, text="Value entry:")
        la1.grid(row=1, column=0)

        userEntry = StringVar()
        global e1
        e1 = Entry(self, width=40, textvariable=userEntry)
        e1.grid(row=1, column=1, columnspan=6)

        e2 = Button(self, text="Enter", command=submit_value)
        e1.delete(0, END)
        e2.grid(row=1, column=5, columnspan=10)


        b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=start_func)
        b1.grid(row=2, column=0)

        b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=change_func)
        b2.grid(row=2, column=3)

        b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=fight_func)
        b3.grid(row=2, column=4)

        b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=pvp_func)
        b4.grid(row=2, column=5)

        b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=loot_func)
        b5.grid(row=2, column=6)

        b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=loot_func)
        b6.grid(row=2, column=7)

        stats = Listbox(self, width= 20)
        stats.insert(1,"health:")
        stats.grid(row=3, column=0, columnspan=8, sticky='news')

root = Tk()
root.title = "Test program"
tw = TestClient(root)
root.mainloop()

0 个答案:

没有答案