Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I use the cryptography package to encrypt and decrypt this socket based chat box. this is just the client side btw #!/usr/bin/env python3

How would I use the cryptography package to encrypt and decrypt this socket based chat box. this is just the client side btw

#!/usr/bin/env python3 """Script for Tkinter GUI chat client."""

from socket import AF_INET, socket, SOCK_STREAM from threading import Thread import tkinter

def receive(): """Handles receiving of messages.""" while True: try: msg = client_socket.recv(BUFSIZ).decode("utf8") msg_list.insert(tkinter.END, 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() my_msg.set("") # Clears input field. client_socket.send(bytes(msg, "utf8")) if msg == "{quit}": client_socket.close() top.destroy() def on_closing(event=None): """This function is to be called when the window is closed.""" my_msg.set("{quit}") send(bytes(my_msg.get(), "utf8")) top = tkinter.Tk() top.title("Chatter")

messages_frame = tkinter.Frame(top) my_msg = tkinter.StringVar() # For the messages to be sent. my_msg.set("Type your messages here.") scrollbar = tkinter.Scrollbar(messages_frame) # To navigate through past messages.

msg_list = tkinter.Listbox(messages_frame, height=15, width=50, 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("", send) entry_field.pack() send_button = tkinter.Button(top, text="Send", command=send) send_button.pack() top.protocol("WM_DELETE_WINDOW", on_closing)

HOST = input('Enter host: ') PORT = input('Enter port: ') if not PORT: PORT = 33000 # Default value. else: PORT = int(PORT) BUFSIZ = 1024 ADDR = (HOST, PORT) client_socket = socket(AF_INET, SOCK_STREAM) client_socket.connect(ADDR)

receive_thread = Thread(target=receive) receive_thread.start() tkinter.mainloop() # Starts GUI execution.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Food Hygiene Auditing

Authors: N. Chesworth

1997th Edition

1461380545, 978-1461380542

More Books

Students also viewed these Accounting questions