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()
时间: 2024-12-17 00:21:52

python upload file的相关文章

jQuery文件上传插件jQuery Upload File 有上传进度条

jQuery文件上传插件jQuery Upload File 有上传进度条 2015年05月15日 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度条显示.标准HTML form文件上传,也就是说,只要服务端支持接收multipart/form-data格式数据就能使用此上传插件. 本站web端文件文件提交即使用此插件,效果如下: 浏览器支持 浏览器支持:IE 8.0,IE 9.0,IE 10.0,Firefox,Saf

Upload file

<h3>Upload File</h3> <form action="@Url.Action("Upload","UploadController")" method="post" id="uploadForm" enctype="multipart/form-data"> <div class="file-box">

Python:file/file-like对象方法详解【单个文件读写】

IO中读写文件操作方法汇总!----The_Third_Wave的学习笔记! 本文由@The_Third_Wave(Blog地址:http://blog.csdn.net/zhanh1218)原创.不定期更新,有错误请指正. Sina微博关注:@The_Third_Wave 如果这篇博文对您有帮助,为了好的网络环境,不建议转载,建议收藏!如果您一定要转载,请带上后缀和本文地址. class file(object) |  file(name[, mode[, buffering]]) -> fi

ubuntu curl upload file to apache2 server

ubuntu curl upload file to apache2 server Table of Contents 1. install 2. get web info 3. set php upload conditions 3.1. ref 3.2. upload_max_fileszie 3.3. post_max_size 3.4. max_execution_time cfg 3.5. restart after cfg 4. config upload directory 5.

MVC-前台调用后台action 传递upload file 参数问题

在后台获取upload file 数量的时候发现count一直为0,经检查发现了问题 ,代码如下: 前台: var data = $("#DetailForm").serialize(); $.ajax({ url: '@Url.Action("SaveRequest", "RegistrationRequest")', type: "POST", dataType: "JSON", data: data,

Express web框架 upload file

哈哈,敢开源,还是要有两把刷子的啊 今天,看看node.js 的web框架 Express的实际应用 //demo1 upload file <html><head><title>文件上传表单</title></head><body><h3>文件上传:</h3>选择一个文件上传: <br /><form action="/file_upload" method="

python爬取豆瓣小组700+话题加回复啦啦啦python open file with a variable name

需求:爬取豆瓣小组所有话题(话题title,内容,作者,发布时间),及回复(最佳回复,普通回复,回复_回复,翻页回复,0回复) 解决:1. 先爬取小组下,所有的主题链接,通过定位nextpage翻页获取总过700+条话题: 2. 访问700+ 链接,在内页+start=0中,获取话题相关的四部分(话题title,内容,作者,发布时间),及最佳回复.回复: 3. 在2的基础上,判断是否有回复,如果有回复才进一步判断是否有回复翻页,回复翻页通过nextpage 获取start=100.start=2

html 5 drag and drop upload file

compatible: chrome firefox ie 11 , not supported demo: http://demo.tutorialzine.com/2011/09/html5-file-upload-jquery-php/ http://www.sitepoint.com/author/craig-buckler/page/23/ http://www.sitepoint.com/html5-file-drag-and-drop/ http://www.sitepoint.c

[Selenium] Upload File on Remote Node Machines

Problem: In web UI automation testing, we always need to upload files, it will work well on local machines, but doesn't work on remote node machines when using Seleniuim Grid to run. Solution: No Description Recommendation Index 1 1.Use PathUtils to