< wxPython GUI

wxPython hálózat

Szerver

szerver.py
import wx
from socket import *
import threading
 
bufsiz = 1024
port = 9000
host = 'localhost'
 
 
class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
 
        self.panel1 = wx.Panel(self)        
        self.textctrl1 = wx.TextCtrl(self.panel1, style=wx.TE_MULTILINE)
 
        self.rootBox = wx.BoxSizer()
        self.rootBox.Add(self.textctrl1, 1, wx.ALL | wx.EXPAND, 5)        
        self.panel1.SetSizerAndFit(self.rootBox)
 
        self.thread = threading.Thread(target=self.Server)
        self.thread.start()
 
    def Print(self, text):
        wx.CallAfter(self.textctrl1.AppendText, text + "\n")
 
    def Server(self):
        self.Print("Port: {}".format(port))
 
        tcpServer = socket(AF_INET , SOCK_STREAM)
        tcpServer.bind((host, port))
        tcpServer.listen(5)
        try:
            while True:
                self.Print("Várakozás kapcsolatra...")
                tcpClient, caddr = tcpServer.accept()
                self.Print("Kapcsolódó kliens {}".format(caddr))
 
                while True:
                    data = tcpClient.recv(bufsiz).decode()
                    if not data:
                        break
                    tcpClient.send(  ('Köszönöm a kapcsolatot\n').encode('utf-8')  )
                    self.Print(data)
                tcpClient.close()
        except KeyboardInterrupt:
            tcpServer.close()
 
app = wx.App(False)
frame = MainFrame(None)
frame.Show()
app.MainLoop()