我正在开发一个应用程序,其中以python编码的kivy gui窗口在按下download时创建了一个套接字,并从服务器下载了视频文件。 但是,下载的视频在gui窗口关闭或最小化之前不会显示在资源管理器中。视频也似乎没有注意到新的视频文件已添加,导致它没有显示此类文件错误。 如何解决呢? ps:这两个文件和一个示例视频文件是在Pycharm中执行的 由于服务器和客户端位于同一目录中,因此附加了“ new”的视频文件将出现在项目文件中。
这是server.py:
import socket import threading import os
def RetrFile(name, sock):
filename = sock.recv(1024)
filename = filename.decode('utf-8')
if os.path.isfile(filename):
sock.send(str.encode('EXISTS') + str(os.path.getsize(filename)).encode('utf-8'))
userResponse = sock.recv(1024)
userResponse = userResponse.decode('utf-8')
if userResponse[:2] == 'OK':
with open(filename, 'rb') as f:
bytesToSend = f.read(1024)
sock.send(bytesToSend)
while bytesToSend != "0":
bytesToSend = f.read(1024)
sock.send(bytesToSend)
else:
sock.send(str.encode("ERR "))
sock.close()
def Main():
host = 'enter your ipv4 address found by typing ipconfig in cmd'
port = 5000
s = socket.socket()
s.bind((host, port))
s.listen(5)
print("Server Started.\nWaiting for a connection...")
while True:
c, addr = s.accept()
print("Client connedted ip:<" + str(addr) + ">")
t = threading.Thread(target=RetrFile, args=("RetrThread", c))
t.start()
s.close()
if __name__ == '__main__':
Main()
这是客户端和基维gui代码:
import socket
def Main():
host = 'enter your ipv4 address found by typing ipconfig in cmd'
port = 5000
s = socket.socket()
s.connect((host, port))
filename = "new_finalvideo.avi"
if filename != 'q':
s.send(filename.encode('utf-8'))
data = s.recv(1024)
data = data.decode('utf-8')
if data[:6] == 'EXISTS':
filesize = int(data[6:])
print("File exists, " + str(filesize) + " bytes, downloading-> ")
if (True):
s.send(str.encode("OK"))
f = open('new_' + filename, 'wb')
data = s.recv(1024)
totalRecv = len(data)
f.write(data)
while (totalRecv) < filesize:
data = s.recv(1024)
totalRecv += len(data)
f.write(data)
print("{0:.2f}".format((totalRecv / float(filesize)) * 100) + "% Done")
print("Download Complete!")
f.close()
else:
print("File Does Not Exist!")
s.close()
#-----------------
import kivy
from kivy.uix.video import Video
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.lang.builder import Builder
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.properties import BooleanProperty
from kivy.animation import Animation
from kivy.base import runTouchApp
hstr="hello from the"
Builder.load_string('''
<ScrolllabelLabel>:
Label:
text: root.text
font_size: 20
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
''')
class ScrolllabelLabel(ScrollView):
text = StringProperty('')
class MyApp(App):
def build(self):
#-----------------
def downloadfunc(instance):
print("button is working")
Main()
return
#video.play = True
def play(instance):
global hstr
hstr=hstr+"hello\n"
lbl1.text=hstr
video.play = True
def change_Vid(self, next_vid):
self.ids.video.vid = next_vid
return self.ids.video.vid
#-----------------
layout = FloatLayout(size=(300, 500))
button1 = Button(
text='download',
size_hint=(None,None),
size=(100,100),
pos=(50, 450),
)
button1.bind(on_press=downloadfunc)
button2 = Button(
text='play',
size_hint=(None, None),
size=(100, 100),
pos=(50, 350),
)
button2.bind(on_press=play)
button3 = Button(
text='change',
size_hint=(None, None),
size=(100, 100),
pos=(50, 50),
)
button3.bind(on_press=change_Vid('new_finalvideo.avi'))
lbl1 = ScrolllabelLabel(text=hstr,
pos=(0, 50),
size_hint=(None, None),
size=(300, 300),
) # create a label instance
layout.add_widget(lbl1)
video = Video(
source=' ',
size_hint=(None,None),
pos=(300, 200),
size=(500, 500),
)
# video.play = True
layout.add_widget(button1)
layout.add_widget(button2)
layout.add_widget(video)
return layout
#---------------
MyApp().run()