Я хочу отправить файл от одного клиента другому клиенту через сервер. Файл выбран из filedialog, в то же время я отправляю обычный текст, но я не могу понять, как мы это делаем с файлом?
Вот что у меня есть
Server.py
from socket import *
from threading import *
import socket
from tkinter import *
clients = set()
def clientThread(clientSocket, clientAddress):
while True:
message = clientSocket.recv(1024)
print(clientAddress[0] + ":" + str(clientAddress[1]) +" says: "+ message)
for client in clients:
if client is not clientSocket:
client.send((clientAddress[0] + ":" + str(clientAddress[1]) +" says: "+ message))
if not message:
clients.remove(clientSocket)
print(clientAddress[0] + ":" + str(clientAddress[1]) +" disconnected")
break
clientSocket.close()
hostSocket = socket.socket(AF_INET, SOCK_STREAM)
host_name = socket.gethostname()
ip=socket.gethostbyname(host_name)
print (ip)
portNumber = 7500
hostSocket.bind((host_name, portNumber))
hostSocket.listen()
print ("Waiting for connection...")
while True:
clientSocket, clientAddress = hostSocket.accept()
clients.add(clientSocket)
print ("Connection established with: ", clientAddress[0] + ":" + str(clientAddress[1]))
thread = Thread(target=clientThread, args=(clientSocket, clientAddress, ))
thread.start()
Client.py
from tkinter import *
from threading import *
from socket import *
from tkinter import filedialog
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
portNumber = 7500
def page1(root):
namelabel = Label(root, text='ChatApp')
namelabel.grid(column=1, row=0)
EDIlabel = Label(root, text='Enter Ip Adress')
EDIlabel.grid(column=0, row=1)
EDIentry = Entry(root)
EDIentry.grid(column=1, row=1)
prtcl = Label(root, text='Protocol')
prtcl.grid(column=0, row=3)
TCPbutton = Radiobutton(root, text='TCP', value=0)
TCPbutton.grid(column=1, row=3)
UDPbutton = Radiobutton(root, text='UDP', value=1)
UDPbutton.grid(column=2, row=3)
def test1():
if len(EDIentry.get())>0:
print((EDIentry.get()))
clientSocket.connect((EDIentry.get(), portNumber))
else:
print ('error')
SCbutton = Button(root, text='Start Connection', command=lambda:[test1(), changepage()])
SCbutton.grid(column=1, row=4)
def page2(root):
txtMessages = Text(root, width=50)
txtMessages.grid(row=0, column=0, padx=10, pady=10)
def searchfile():
global name
nm=filedialog.askopenfile(mode='rb')
name=nm
print (name)
name_data=name.read(1024)
print (name_data)
srchfl = Button(root, text="Browse", width=20, command=searchfile)
srchfl.grid(row=2, column=1, padx=10, pady=10)
txtYourMessage = Entry(root, width=50)
txtYourMessage.insert(0,"Your message")
txtYourMessage.grid(row=1, column=0, padx=10, pady=10)
root.title("Connected To: " + str(portNumber))
def sendMessage():
clientMessage = txtYourMessage.get()
txtMessages.insert(END, "\n" "client : " + clientMessage)
clientSocket.send((clientMessage.encode("utf-8")))
filedata=name.read()
print (filedata)
btnSendMessage = Button(root, text="Send", width=20, command=sendMessage)
btnSendMessage.grid(row=2, column=0, padx=10, pady=10)
def recvMessage():
while True:
serverMessage = clientSocket.recv(1024).decode("utf-8")
print(serverMessage)
txtMessages.insert(END, "\n"+serverMessage)
recvThread = Thread(target=recvMessage)
recvThread.daemon = True
recvThread.start()
def changepage():
global pagenum, root
for widget in root.winfo_children():
widget.destroy()
if pagenum == 1:
page2(root)
pagenum = 2
else:
page1(root)
pagenum = 1
pagenum = 1
root = Tk()
page1(root)
root.mainloop()