import socket
def main():
? ? '''文件下載器客戶端優(yōu)化'''
? ? # 創(chuàng)建套接字
? ? cli_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
? ? # 連接服務(wù)器
? ? cli_soc.connect(('127.0.0.1', 8080))
? ? # 發(fā)送文件名
? ? file_name = input('請輸入文件名 :')
? ? cli_soc.send(file_name.encode('utf-8'))
? ? # 接受文件內(nèi)容
? ? file_data = cli_soc.recv(1024*1024)
? ? if file_data:
? ? ? ? # 保存內(nèi)容到本地文件
? ? ? ? with open(file_name, 'wb') as file:
? ? ? ? ? ? file.write(file_data)
? ? else:
? ? ? ? print('服務(wù)器數(shù)據(jù)為空或渤,文件不存在')
? ? # 關(guān)閉套接字
? ? cli_soc.close()
if __name__ == '__main__':
? ? main()
#如有錯(cuò)誤之處,還望大家指正奕扣,謝謝劳坑!