1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import tkinter as tk
import socket
HOSTPORT = ('localhost',1234)
BUFSIZE = 1024
CODE = 'utf8'
class App(tk.Tk) :
def __init__(self) :
tk.Tk.__init__(self)
self.geometry('100x100')
# Create socket
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind(HOSTPORT)
# Start connection
button = tk.Button(self, text='Listen', command=self.listen)
button.pack()
# Close GUI, even if connection opened
button = tk.Button(self, text='Close program', command=self.destroy)
button.pack()
def listen(self) :
while True :
self.socket.listen(5)
conn, address = self.socket.accept()
data = conn.recv(BUFSIZE)
print('Client says %s'%data.decode(CODE))
conn.close()
self.socket.close()
app = App()
app.mainloop() |
Partager