客户端代码:
#-*-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)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年11月20日
2024年11月20日
- 容祖儿《小小》香港首版 [WAV+CUE][1.1G]
- 莫文蔚《拉活…》SONY [WAV+CUE][1G]
- Beyond《极品天碟》LPCD45II首批限量版[WAV+CUE][1.7G]
- HIFI示范巅峰之作《情解药·Hi-Fi心魂》2CD[WAV+CUE]
- 房东的猫2021-关于彻夜不眠的事情(EP)[青柴文化][WAV+CUE]
- 群星.1993-一曲成名·青春无悔【飞碟】【WAV+CUE】
- 张芸京.2016-失败的高歌【泡耳音乐】【WAV+CUE】
- 天籁女声《2024第31届上海国际高端音影展纪念CD》[WAV+CUE][1.1G]
- 姚斯婷 《敢爱敢做》头版限量编号24K金碟[低速原抓WAV+CUE][1.2G]
- 雷婷 《把爱留在昨天》紫银合金AQCD[低速原抓WAV+CUE][1.1G]
- 董文华2024-《精选30年·长城长HQ》头版限量[WAV+CUE]
- 柏菲·魏松2024-《跟你走》限量开盘母带ORMCD[WAV+CUE]
- 柏菲·甘雅丹《雅鲁藏布》限量开盘母带ORMCD[WAV+CUE]
- 孙露《明天你是否依然爱我》1:1母盘直刻[低速原抓WAV+CUE][1G]
- 群星2024《龙年精选.音乐盛宴》纯银CD[WAV+CUE][1.1G]