python - Python 通过套接字读取和发送文件
问题描述
我有一个服务器,它连接了 20 多个客户端。如果需要,我希望它更新这些客户端。我发现的解决方案太复杂了,或者根本不起作用,所以我认为它可能会起作用:
- 服务器读取“client.py”并通过套接字发送。
- 客户端接受数据,创建目录“更新”,将文件保存在那里并运行 bash 脚本。
- Bash 脚本会停止客户端,将其删除,然后将新客户端从 /update 移动到主目录。
问题是它在约 70% 的情况下有效。客户端通常在更新后工作正常。问题在于 30% 的客户端只接收文件的一部分。假设我的客户是 5kB,70% 的客户很好,30% 的客户只收到 4kB 并且 ofc 不能再运行了。
服务器:
def updateClient(self):
try:
file = open(str(pathlib.Path(_file_).parent.resolve())+"/client.py", "r")
newcl = file.read()
for host in self.active_connections:
try:
host.connection.send(newcl.encode())
except Exception as e:
print("[!]Error!: " + str(e))
客户:
data = s.recv(8192)
data = data.decode()
newdir_path = str(pathlib.Path(_file_).parent.resolve())
newcl_path = newdir_path+"/update/client.py"
try:
os.mkdir(newdir_path+"/update")
print("Update directory created")
file = open(newcl_path, 'w')
file.write(data)
file.close()
print("[*] Update downloaded!")
try:
process = subprocess.run("update.sh", shell=False, check=True)
except Exception as e:
print(str(e))
解决方案
推荐阅读
- bash - 为什么 bash 为 http 链接添加单引号?
- python-3.x - GCD 和 LCM 的功能
- python - 检查输入时出错:预期 conv1d_57_input 有 3 个维度,但得到的数组形状为 (152, 64)
- javascript - 未捕获的类型错误:无法读取未定义 SweetAlert 和 jquery 的属性“then”
- azure-devops - 在 Azure DevOps 发布管道中,如何将最新版本提升到一段时间内没有发布任何版本的阶段?
- r - 条件匹配时从不同行填充值
- python - 如何告诉 tkinter 应用程序只运行一个实例?
- python - 绘制椭圆的方程,不起作用
- c# - 如何使用 GetAttribute 方法检索元素文本?
- reactjs - 用于 Web 键盘快捷键的 Electron React Native