python upload file

Posted fengjian1585

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python upload file相关的知识,希望对你有一定的参考价值。

server.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import SocketServer
import os

class MySocketServer(SocketServer.BaseRequestHandler):

    def handle(self):
        filename_dir = /home/feng/feng
        conn = self.request
        data = conn.recv(1024)
        filename,filename_size = data.strip().split(|)

        print filename,filename_size

        recv_size = 0

        file_dir=os.path.join(filename_dir,filename)

        f = open(file_dir,w+)

        while True:
            if int(filename_size) != recv_size :
                data = conn.recv(1024)
                recv_size += len(data)
                print int(filename_size),recv_size
                f.write(data)
            else:
                break
            # else:
            #     data = conn.recv(int(filename_size) - recv_size)
            #     print int(filename_size) - recv_size
            #     break
        print upload successed.......

if  __name__ == __main__:
    ip_port = (127.0.0.1,55555)
    server = SocketServer.ThreadingTCPServer(ip_port,MySocketServer)
    server.serve_forever()



=======================================
client.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import socket
import os

client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip_port = (127.0.0.1,55555)
client.connect(ip_port)

while True:
    filepath = raw_input(请输入方法和上传路径:/root/1.txt:  )
    filename_size = os.stat(filepath).st_size
    filename = os.path.basename(filepath)
    
    print #####################
    print filepath,filename_size
    print #####################

    client.send(filename + | + str(filename_size))
    send_size = 0


    f = open(filepath,r)

    Flag = True
    while Flag:
        if filename_size != send_size:
            data = f.read(1024)
            send_size += len(data)
            client.send(data)
        else:
            Flag=False
    f.close()
    client.close()

 

以上是关于python upload file的主要内容,如果未能解决你的问题,请参考以下文章

无法使用ngf-resize来使用ng-file-upload

关于PHP上传文件时配置 php.ini 中的 upload_tmp_dir

如何使用带有apollo-upload-client的graphene-file-upload将graphql中的文件上传到Python数据库并在前端做出反应。?

已解决在react+ts中 atnd 用 upload 组件报错Failed to execute ‘readAsArrayBuffer,param 1 is notof type Blob(代码片段

已解决在react+ts中 atnd 用 upload 组件报错Failed to execute ‘readAsArrayBuffer,param 1 is notof type Blob(代码片段

jQuery File Upload文件上传插件使用