通过Python套接字发送Zip文件夹

2024-10-01 09:41:56 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,出于某种原因,该代码无法正确地将zip文件夹从客户端发送到服务器。在服务器端,如果我使用“f=zipfile.zipfile”(“platformIO.zip”),我会得到错误“ValueError:stat:embedded null character in path”,如果我使用“f=open”(“platformIO.zip”,“wb”),则不会抛出错误,但收到的文件已损坏且不会打开

我已经阅读了与此类似的所有问题,但找不到解决方案

客户:

import socket
import time


# Configure wireless connection with server (board on leg)
HOST = "192.168.4.1" # change to server ip address
PORT = 5005 # must be same as server

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print("connected")

user_input = "startup"

while True:
    if user_input == "startup":
        # ask if user wants to send code
        user_input = input("Do you want to send new code?: (y/n)")
        if user_input == "y":
            user_input = "send code"        
        elif user_input == "n":
            user_input = input("Would you like to collect data?: (y/n)")
            if user_input == "y":
                user_input = "receive data"
            else:
                user_input = "startup"
        else:
            user_input == "startup" 
    elif user_input == "send code":
        st = "sending code"
        s.send(st.encode())
        file = "platformIO.zip"
        try:
            with open(file, "rb") as f:
                print("sending file...")
                data = f.read()
                s.sendall(data)
            print("finished sending")
            st = "finished"         
            s.send(st.encode())
            user_input = "startup"      
        except:
            print("Failed transfering <platformIO.zip>, make sure it exists")
            st = "failed"           
            s.send(st.encode())
    elif input =="receive data":
        print("feature not yet implemented")
        user_input == "startup"
    # delay
    time.sleep(0.1)

服务器:

import socket
import time
import zipfile
import os

# create server
HOST = "192.168.4.1"
PORT = 5005
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("socket created")

# managing error exception
try:
    s.bind((HOST,PORT))
except socket.error:
    print("bind Failed")

s.listen(1)
print("Socket awaiting mesesages")
(conn, addr) = s.accept()
print("connected")

# awaiting message
msg = "startup"

while True:
    if msg == "startup":
        # receive data from controller
        msg = conn.recv(4096)
        print(str(msg, 'utf-8'))
    elif str(msg, 'utf-8') == "sending code":
        f = zipfile.ZipFile("platformIO.zip", "w")
        #f = open("platformIO.zip", "wb")   
        while True:         
            data = conn.recv(4096)
            print(data)         
            if not data:
                break
            f.write(data)
        print("file received")  
        msg = "startup"
    time.sleep(.1)

conn.close()

编辑:如果我使用f=open(“platformIO.zip”、“wb”)并在服务器的writing while循环中添加s.close(),我可以成功接收zip,但随后连接关闭,在关闭程序之前我无法打开zip文件

    f = open("platformIO.zip", "wb")    
    while True:         
        data = conn.recv(4096)
        print(data)         
        if not data:
            break
        f.write(data)
        s.close()

Tags: importsendinputdataifcodemsgsocket