【问题标题】:Trying to make a Server application with socket and tkinter尝试使用套接字和 tkinter 制作服务器应用程序
【发布时间】:2020-11-01 01:38:07
【问题描述】:

我试图让这个 Socket & tkinter 服务器应用程序以简写形式显示,这是一个 tk 窗口,它基本上是一个带有一些 Buttons 和一些 Entrys 的套接字服务器。问题是服务器制造商无法正常工作。这就是我填写条目时编写的脚本的含义,当我按下“创建服务器”按钮时,我应该得到一个print("Server Made!"),但我得到的是 Python is not responding 哇,这些不是最好的词吗?我只是在开玩笑。

这是我的代码/脚本:

from tkinter import *
from win32api import GetSystemMetrics
import time
import socket








def Client(ip,port,Cname):
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect((str(ip), int(port)))
        msg = client.recv(4096)
        print(msg.decode("utf-8"))




#Server Dashboard
def __main__SD():
    tk = Tk()
    tk.attributes('-fullscreen',True)
    
    tk.title('SECL-Dashboard')
    
    x = int(GetSystemMetrics(0))
    y = int(GetSystemMetrics(1))
    
    
    Top_Frame = Frame(width=x,height=50,bg='orange')
    Top_Frame.place(x=0,y=0)
    
    X_Button = Button(tk,width=5,height=1,text='X',bg='red',fg='white',command=tk.destroy)
    X_Button.place(x=1500,y=2)
    
    X_Button.config(font =("Bold", 16))
    
    
    tk.mainloop()
    
    print("Exiting")
    


    
    


# Config-CLIENT
def CONFIG2():
    root = Tk()
    root.resizable(width=False, height=False)
    
    root.title("SECL-Client")
    root.geometry('400x500')
    x = Frame(height = 60,width = 600,bg='green')
    x.pack()
    
    TITLE = Label(root,text = "SECL",bg='green',fg='white')
    TITLE.place(x=140,y=4)
    TITLE.config(font =("Bold", 30))
    
    TP = Label(root,text='Client',bg='green',fg='white')
    TP.place(x=252,y=30)
    TP.config(font=("Bold",15))
    
    #Create Button
    
    JoinB = Button(root,width=12,height=2,text='Join Server',bg='#00FF00',command=lambda:[root.destroy(),__main__SD()]) #Server Join
    JoinB.place(x=90,y=440)
    
    
    #Cancle Button
    CancelB = Button(root,width=12,height=2,text='Cancel',bg='#D3D3D3',command=root.destroy)
    CancelB.place(x=200,y=440)
    
    CB = Button(root,width=5,height=2,text='<',bg='#FFFFFF',command=lambda:[root.destroy(),CONFIG1()])
    CB.place(x=350,y=455)
    CB.config(font=("Bold",10))
    
    
    #Join
    port_I = Label(root,text='Port:')
    port_I.place(x=100,y=145)
    port_I.config(font =("Bold", 15))
    
    
    IP_I = Label(root,text='IP:')
    IP_I.place(x=100,y=200)
    IP_I.config(font =("Bold", 15))
    
    
    CN_I = Label(root,text='Client name:')
    CN_I.place(x=30,y=245)
    CN_I.config(font =("Bold", 15))
    
    
    
    ip = Entry(root,fg='black')
    ip.place(width=155,height=20)
    ip.place(x=150,y=200)    
    
    
    port = Entry(root,fg='black')
    port.place(width=155,height=20)
    port.place(x=150,y=150)
    
    
    cn = Entry(root,fg='black')
    cn.place(width=155,height=20)
    cn.place(x=150,y=245)
    root.mainloop()



#Config-SERVER
def CONFIG1():
    root = Tk()
    root.resizable(width=False, height=False)
    
    root.title("SECL-Config")
    root.geometry('400x500')
    x = Frame(height = 60,width = 600,bg='blue')
    x.pack()
    
    TITLE = Label(root,text = "SECL",bg='blue',fg='white')
    TITLE.place(x=140,y=4)
    TITLE.config(font =("Bold", 30))
    
    TP = Label(root,text='Server',bg='blue',fg='white')
    TP.place(x=252,y=30)
    TP.config(font=("Bold",15))
    
    
    
    #Cancle Button
    CancleB = Button(root,width=12,height=2,text='Cancel',bg='#D3D3D3',command=root.destroy)
    CancleB.place(x=200,y=440)
    
    
    #Client Button
    
    CB = Button(root,width=5,height=2,text='>',bg='#FFFFFF',command=lambda:[root.destroy(),CONFIG2()])
    CB.place(x=350,y=455)
    CB.config(font=("Bold",10))
    
    
    # INFO
    port_I = Label(root,text='Port:')
    port_I.place(x=80,y=145)
    port_I.config(font =("Bold", 15))
    
    
    
    Name_I = Label(root,text='Server Name:')
    Name_I.place(x=10,y=200)
    Name_I.config(font =("Bold", 15))
    
    
    MClient_I = Label(root,text="Max Client:")
    MClient_I.place(x=20,y=245)
    MClient_I.config(font =("Bold", 15))
    
    
    
    
    
    port = Entry(root,fg='black')
    port.place(width=155,height=20)
    port.place(x=150,y=150)
    
    
    name = Entry(root,fg='black')
    name.place(width=155,height=20)
    name.place(x=150,y=200)
    
    
    mclient = Entry(root,fg='black')
    mclient.place(width=155,height=20)
    mclient.place(x=150,y=245)
    
    
    def ALL_POINT():
        serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        hostname = socket.gethostname()
        ip = socket.gethostbyname(hostname)
        serv.bind((str(ip), int(port.get())))
        serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        serv.listen(int(mclient.get()))
    
        conn, addr = serv.accept()
            #data = conn.recv(int(max_people))
        conn.send(bytes(f"Welcome to {name.get()}!",'utf-8'))
        print("Server: Running")
        

    #Create Button
    #CreateB = Button(root,width=12,height=2,text='Create Server',bg='#00FF00',command=lambda:[root.destroy(),ALL_POINT()])
    CreateB = Button(root,width=12,height=2,text='Create Server',bg='#00FF00',command=ALL_POINT)
    CreateB.place(x=90,y=440)
    
    
    
    
    root.mainloop()
    


CONFIG1()

没有错误没什么,但我只是明白了Python is not responding 有没有办法可以修复或更好地从我的脚本中删除这个错误? 顺便说一句,我正在运行 Windows 7。

【问题讨论】:

  • 这是因为serv.accept()是一个阻塞函数。您应该改为在子线程中运行 ALL_POINT()
  • 能否提供一些代码:)

标签: python tkinter python-sockets


【解决方案1】:

由于serv.accept()是一个阻塞函数,它会阻止tkinter mainloop处理事件。

您需要在子线程中运行ALL_POINT()

import threading
...

def CONFIG1():
    ...

    server_running = False

    # sample client handler
    def handle_client(conn, addr):
        conn.send(bytes(f"Welcome to {name.get()}!",'utf-8'))
        while True:
            data = conn.recv(512).decode().strip()
            print(data)
            if data == 'quit':
                break
        conn.close()

    def ALL_POINT():
        nonlocal server_running

        serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        hostname = socket.gethostname()
        ip = socket.gethostbyname(hostname)
        print(ip, port.get())
        serv.bind((str(ip), int(port.get())))
        serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        serv.listen(int(mclient.get()))
    
        print("Server: Running")
        server_running = True
        while server_running:
            print('waiting connection ...')
            conn, addr = serv.accept()
            print('client connected', addr)
            # run client handler in thread so that server can serve new connection
            threading.Thread(target=handle_client, args=(conn, addr), daemon=True).start()

    def start_server():
        # make sure only one server task is active
        if not server_running:
            threading.Thread(target=ALL_POINT, daemon=True).start()

    CreateB = Button(root, width=12, height=2, text='Create Server',
                     bg='#00FF00', command=start_server)

【讨论】:

  • 我仍然得到Python is not responding,但当我填写条目并按下按钮时会发生这种情况,它会在输出栏中显示我的 ip 和我正在使用的端口。之后我得到Python is not responding
  • 我也无法通过另一台电脑连接到服务器!
  • 用您所做的更改更新问题。
  • 使用您根据我的解决方案所做的更改更新发布的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 2011-01-22
  • 2012-02-20
  • 2021-08-12
  • 2015-03-15
  • 2013-03-20
相关资源
最近更新 更多