python 并发传输文件(python单线程文件传输的实例C/S)
类别:脚本大全 浏览量:440
时间:2022-03-29 10:44:24 python 并发传输文件
python单线程文件传输的实例C/S客户端代码:
|
#-*-encoding:utf-8-*- import socket import os import sys import math import time def progressbar(cur, total): percent = '{:.2%}' . format ( float (cur) / float (total)) sys.stdout.write( '\r' ) sys.stdout.write( "[%-50s] %s" % ( '=' * int (math.floor(cur * 50 / total)), percent)) sys.stdout.flush() def getfilesize( file ): file .seek( 0 , os.seek_end) filelength = file .tell() file .seek( 0 , 0 ) return filelength def getfilename(filefullpath): index = filefullpath.rindex( '\\' ) if index = = - 1 : return filefullpath else : return filefullpath[index + 1 :] def transferfile(): filefullpath = r "%s" % raw_input ( "file path: " ).strip( "\"" ) if os.path.exists(filefullpath): timestart = time.clock() file = open (filefullpath, 'rb' ) filesize = getfilesize( file ) client = socket.socket(socket.af_inet, socket.sock_stream) client.connect((targethost, targetport)) # send file size client.send( str (filesize)) response = client.recv( 1024 ) # send file name client.send(getfilename(filefullpath)) response = client.recv( 1024 ) # send file content sentlength = 0 while sentlength < filesize: buflen = 1024 buf = file .read(buflen) client.send(buf) sentlength + = len (buf) process = int ( float (sentlength) / float (filesize) * 100 ) progressbar(process, 100 ) client.recv( 1024 ) file .close() timeend = time.clock() print "\r\nfinished, spent %d seconds" % (timeend - timestart) else : print "file doesn't exist" targethost = raw_input ( "server ip address: " ) targetport = int ( raw_input ( "server port: " )) while true: transferfile() |
服务器端代码:
|
#-*-encoding:utf-8-*- import socket import threading import os import sys import math bindip = "0.0.0.0" bindport = 9999 server = socket.socket(socket.af_inet, socket.sock_stream) server.bind((bindip, bindport)) server.listen( 1 ) print "listening on %s:%d" % (bindip, bindport) def progressbar(cur, total): percent = '{:.2%}' . format ( float (cur) / float (total)) sys.stdout.write( '\r' ) sys.stdout.write( "[%-50s] %s" % ( '=' * int (math.floor(cur * 50 / total)), percent)) sys.stdout.flush() def checkfilename(originalfilename): extensionindex = originalfilename.rindex( "." ) name = originalfilename[:extensionindex] extension = originalfilename[extensionindex + 1 :] index = 1 newnamesuffix = "(" + str (index) + ")" finalfilename = originalfilename if os.path.exists(finalfilename): finalfilename = name + " " + newnamesuffix + "." + extension while os.path.exists(finalfilename): index + = 1 oldsuffix = newnamesuffix newnamesuffix = "(" + str (index) + ")" finalfilename = finalfilename.replace(oldsuffix, newnamesuffix) return finalfilename def handleclient(clientsocket): # receive file size filesize = int (clientsocket.recv( 1024 )) # print "[<==] file size received from client: %d" % filesize clientsocket.send( "received" ) # receive file name filename = clientsocket.recv( 1024 ) # print "[<==] file name received from client: %s" % filename clientsocket.send( "received" ) filename = checkfilename(filename) file = open (filename, 'wb' ) # receive file content print "[==>] saving file to %s" % filename receivedlength = 0 while receivedlength < filesize: buflen = 1024 if filesize - receivedlength < buflen: buflen = filesize - receivedlength buf = clientsocket.recv(buflen) file .write(buf) receivedlength + = len (buf) process = int ( float (receivedlength) / float (filesize) * 100 ) progressbar(process, 100 ) file .close() print "\r\n[==>] file %s saved." % filename clientsocket.send( "received" ) while true: client, addr = server.accept() print "[*] accepted connection from: %s:%d" % (addr[ 0 ], addr[ 1 ]) clienthandler = threading.thread(target = handleclient, args = (client,)) clienthandler.start() |
运行结果示例:
服务器端:
客户端(服务器端做了端口映射:59999->9999):
以上这篇python单线程文件传输的实例(c/s)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/qwertyupoiuytr/article/details/65667552
您可能感兴趣
- python中的冒号怎么看(python 列表中[ ]中冒号‘:’的作用)
- python彩色字符视频代码(python将视频转换为全字符视频)
- python比go语言简单(Python和Go语言的区别总结)
- pythonssh登录服务器(对python 通过ssh访问数据库的实例详解)
- python简单代码实例(Python实现 版本号对比功能的实例代码)
- python下载后依然打不开文件(解决python打不开文件文件不存在的问题)
- linux查python进程(linux查找当前python解释器的位置方法)
- python实例教程(Python魔法方法详解)
- python ssh登录服务器(python利用跳板机ssh远程连接redis的方法)
- python 自定义获取文件目录(Python使用os.listdir和os.walk获取文件路径与文件下所有目录的方法)
- python如何判断文件是否结束(python判断文件是否存在,不存在就创建一个的实例)
- python把str转成list(python3 字符串/列表/元组str/list/tuple相互转换方法及join函数的使用)
- python数组与矩阵转换(python 读取文件并把矩阵转成numpy的两种方法)
- python外部如何调嵌套函数(python中嵌套函数的实操步骤)
- python获取微信用户(python-itchat 获取微信群用户信息的实例)
- python控制流实例(如何用C代码给Python写扩展库Cython)
- 爱情是什么(爱情是什么最经典的话)
- 乔欣 古装剧中的高颜值(古装剧中的高颜值)
- 怎么才可以财富自由(如何让自己实现财富自由)
- 为什么越来越多年轻人回农村(为什么越来越多年轻人回农村生活)
- 怎么快速学好英语(怎么快速学好英语初中)
- 中国留学生都是富二代吗()
热门推荐
- mysql清空数据库所有表格(MySQL用truncate命令快速清空一个数据库中的所有表)
- php常用加密技术(php用户名的密码加密更安全的方法)
- mysql连接navicat报错1045(Navicat 连接MySQL8.0.11出现2059错误)
- mysql8使用技巧(一篇学会MySQL 8.0 ROLE管理)
- js中Document
- 欧洲vps服务器速度与稳定怎么判断?(欧洲vps服务器速度与稳定怎么判断?)
- jquery五子棋javascript(原生JavaScript实现简单五子棋游戏)
- nginx流媒体服务搭建与应用(用nginx+FastDFS一步步搭建文件管理系统)
- laravel框架怎么获取变量(解决laravel id非自增 模型取回为0 的问题)
- C# Checklistbox的用法
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9