我有一个服务器,可以从他的每个客户端接收文件,但是我想按升序保存每个文件,例如file1.txt,file2.txt等。
我接受文件的服务器部分是
def getfile(self):
count = count+1
g = open('from_client'+count+'.txt','wb')
while True:
print('receiving data...')
data = self.sock.recv(BUFFER_SIZE)
print('data=%s', (data))
if not data:
g.close()
print('Successfully get the file')
self.sock.close()
break
# write data to a file
g.write(data)
如何每次使用不同的文件名保存它?
答案 0 :(得分:0)
我必须将计数器转换为带有str(count)的字符串,并将count = 0放置在班级之外
def getfile(self,count):
count = count+1
g = open('from_client'+str(count)+'.txt','wb')
while True:
print('receiving data...')
data = self.sock.recv(BUFFER_SIZE)
print('data=%s', (data))
if not data:
g.close()
print('Successfully get the file')
self.sock.close()
break