import tkinter as tk
import socket
from _thread import *
c_list = []
close = False
server_socket = None
def createNewWindow():
def threaded(client_socket, addr):
global chat_log
chat_log['state'] = 'normal'
chat_log.insert("end", 'Connected by :' + addr[0] + ':' + str(addr[1]) + '\n')
chat_log['state'] = 'disabled'
for c in c_list:
c.sendall(('[System] ' + str(addr[1]) + ' 님이 접속하였습니다.').encode())
while 1:
try:
data = client_socket.recv(1024)
chat_log['state'] = 'normal'
chat_log.insert("end",
'Received from ' + addr[0] + ' : ' + str(addr[1]) + ' :: ' + str(data.decode()) + '\n')
chat_log['state'] = 'disabled'
for c in c_list:
print(c)
print(client_socket)
print(1)
c.sendall((str(addr[1]) + ' : ' + data.decode()).encode())
except ConnectionResetError as e:
c_list.remove(client_socket)
for c in c_list:
c.sendall(('[System] ' + str(addr[1]) + ' 님이 나갔습니다.').encode())
chat_log['state'] = 'normal'
chat_log.insert("end", 'Disconnected by ' + addr[0] + ':' + str(addr[1]) + '\n')
chat_log['state'] = 'disabled'
break
client_socket.close()
def server_open():
HOST = ip_entry.get()
PORT = int(port_entry.get())
start_new_thread(make_server, (HOST, PORT))
open_button['state'] = 'disabled'
ip_entry['state'] = 'readonly'
port_entry['state'] = 'readonly'
def server_close():
exit()
def make_server(HOST, PORT):
global server_socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 포트사용중이라 연결할 수 없다는 WinError 10048 에러를 해결하기 위해 필요합니다.
# 서버 소켓의 SOL_SOCKET의 SO_REUSEADDR(이미 사용중인 포트에 대해서도 바인드 허용) 를 1(True)로 설정하는 것으로 이해
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print(server_socket)
server_socket.bind((HOST, PORT))
server_socket.listen()
chat_log['state'] = 'normal'
chat_log.insert("end", 'Server Start\n')
chat_log['state'] = 'disabled'
while 1:
print(1)
client_socket, addr = server_socket.accept()
print(client_socket, '111', addr)
c_list.append(client_socket)
start_new_thread(threaded, (c_list[-1], addr))
s_root = tk.Toplevel(root)
s_root.geometry('500x500')
s_root.title('Server')
s_root.resizable(False, False)
''' Top Menu '''
tk.Label(s_root, text='Server IP : ').place(x=20, y=20)
tk.Label(s_root, text='Port : ').place(x=250, y=20)
ip_entry = tk.Entry(s_root, width=14, text='14.33.178.121')
ip_entry.place(x=83, y=21)
ip_entry.insert(0, '14.33.178.121')
port_entry = tk.Entry(s_root, width=5, text='9999')
port_entry.place(x=290, y=21)
port_entry.insert(0, '9999')
open_button = tk.Button(s_root, text='Server Open', command=server_open)
open_button.place(x=380, y=18)
''' Middle Menu '''
chat_log = tk.Text(s_root, width=65, height=29, state='disabled', spacing2=2)
chat_log.place(x=20, y=60)
''' Bottom Menu '''
close_button = tk.Button(s_root, text='Server Close', command=server_close)
close_button.place(x=200, y=460)
s_root.mainloop()
root = tk.Tk()
root.title('login')
root.geometry('200x200')
lbl = tk.Label(root, text="server IP")
lbl.grid(row=0, column=0)
txt = tk.Entry(root)
txt.grid(row=0, column=1)
lbl = tk.Label(root, text="port")
lbl.grid(row=1, column=0)
txt = tk.Entry(root)
txt.grid(row=1, column=1)
btn = tk.Button(root, text="login", command=createNewWindow)
btn.grid(row=2, column=0)
root.mainloop()
http://pythonstudy.xyz/python/article/122-Tkinter-%EC%9D%B4%EB%B2%A4%ED%8A%B8
top of page
기능을 테스트하려면 라이브 사이트로 이동하세요.
2022.09.03
2022.09.03
댓글 0개
좋아요
댓글(0)
bottom of page