python3_ftp多线程上传图片
项目中研发人员自己写了ftp服务,没有标准ftp中的列表,准备用jmeter对ftp压力测试时发现jmeter要验证列表(如果有同学用jmeter测试过类似的分享一下方法谢谢了),没办法只能用python编写一个多线程的脚本对ftp进行简单的压力测试,项目也只需要一个粗略的结果
#!/usr/bin/python3
from ftplib import FTP
import time
import threading
import random
import sys threadLock = threading.Lock()
g_count = 0 def ftpconnect(host, username, password):
ftp = FTP()
ftp.set_debuglevel(2)
ftp.connect(host, 2008)
ftp.login(username, password)
return ftp def downloadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'wb')
ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize)
ftp.set_debuglevel(0)
fp.close() def uploadfile(ftp, localpath, remotepath):
bufsize = 1024
fp = open(localpath, 'rb')
ftp.storbinary('STOR ' + remotepath, fp, bufsize)
ftp.set_debuglevel(0)
fp.close() # def print_time(threadName, delay, counter):
# while counter:
# time.sleep(delay)
# print("%s: %s" % (threadName, time.ctime(time.time())))
# counter -= 1 def testuploadfile(threadName):
global threadLock
global g_count
ftp = ftpconnect("10.1.1.61", "admin", "admin")
start_tick = time.time()
count = 0
while True:
pic_name = '51010000491320000001/' + threadName + '_' + str(count) + '.jpg'
uploadfile(ftp, "/home/xzrs/ftp/face1.jpg", pic_name)
end_tick = time.time()
count = count + 1
if (end_tick - start_tick) > 1.0:
break
ftp.quit()
print(threadName+': count='+str(count))
threadLock.acquire()
g_count = g_count + count
threadLock.release() def virtualUploadFile():
global threadLock
global g_count
start_tick = time.time()
count = 0
while True:
num = random.random()
time.sleep(num)
end_tick = time.time()
count = count + 1
if (end_tick - start_tick) > 1.0:
break
threadLock.acquire()
g_count = g_count + count
threadLock.release() class MyThread(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name def run(self):
print("start:"+self.name)
testuploadfile(self.name)
# virtualUploadFile()
print("end:"+self.name) if __name__ == "__main__":
threadList = []
#for i in range(0,int(sys.argv[1])):
for i in range(0,10):
thread = MyThread(i, "Thread_" + str(i))
thread.start()
threadList.append(thread)
for t in threadList:
t.join()
print(sys.argv)
print("main exit...")
print('g_count='+str(g_count))
在服务器终端运行结果:
python3 ftp_threading.py |tee log.log
start:Thread_0
start:Thread_1
start:Thread_2
start:Thread_3
start:Thread_4
start:Thread_5
start:Thread_6
start:Thread_7
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
start:Thread_8
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
start:Thread_9
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '227 Entering Passive Mode(10,1,1,61,173,162)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,173,162)'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*get* '227 Entering Passive Mode(10,1,1,61,181,118)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,181,118)'
*get* '227 Entering Passive Mode(10,1,1,61,132,196)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,132,196)'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '227 Entering Passive Mode(10,1,1,61,165,174)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,165,174)'
*cmd* 'STOR 51010000491320000001/Thread_8_0.jpg'
*put* 'STOR 51010000491320000001/Thread_8_0.jpg\r\n'
*get* '227 Entering Passive Mode(10,1,1,61,156,145)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,156,145)'
*cmd* 'STOR 51010000491320000001/Thread_0_0.jpg'
*put* 'STOR 51010000491320000001/Thread_0_0.jpg\r\n'
*get* '220 welcome\n'
*resp* '220 welcome'
*cmd* 'USER admin'
*put* 'USER admin\r\n'
*cmd* 'STOR 51010000491320000001/Thread_7_0.jpg'
*put* 'STOR 51010000491320000001/Thread_7_0.jpg\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*cmd* 'STOR 51010000491320000001/Thread_2_0.jpg'
*put* 'STOR 51010000491320000001/Thread_2_0.jpg\r\n'
*get* '227 Entering Passive Mode(10,1,1,61,145,53)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,145,53)'
*cmd* 'STOR 51010000491320000001/Thread_1_0.jpg'
*put* 'STOR 51010000491320000001/Thread_1_0.jpg\r\n'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*get* '227 Entering Passive Mode(10,1,1,61,162,27)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,162,27)'
*get* '227 Entering Passive Mode(10,1,1,61,146,15)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,146,15)'
*get* '227 Entering Passive Mode(10,1,1,61,156,4)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,156,4)'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*cmd* 'STOR 51010000491320000001/Thread_5_0.jpg'
*put* 'STOR 51010000491320000001/Thread_5_0.jpg\r\n'
*cmd* 'STOR 51010000491320000001/Thread_4_0.jpg'
*put* 'STOR 51010000491320000001/Thread_4_0.jpg\r\n'
*get* '331 need password\n'
*resp* '331 need password'
*cmd* 'PASS *****'
*put* 'PASS *****\r\n'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*cmd* 'STOR 51010000491320000001/Thread_6_0.jpg'
*put* 'STOR 51010000491320000001/Thread_6_0.jpg\r\n'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*get* '230 welcome to my ftp!\n'
*resp* '230 welcome to my ftp!'
*cmd* 'STOR 51010000491320000001/Thread_3_0.jpg'
*put* 'STOR 51010000491320000001/Thread_3_0.jpg\r\n'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '200 TYPE set to I.\n'
*resp* '200 TYPE set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*get* '227 Entering Passive Mode(10,1,1,61,143,118)\n'
*resp* '227 Entering Passive Mode(10,1,1,61,143,118)'
*cmd* 'STOR 51010000491320000001/Thread_9_0.jpg'
*put* 'STOR 51010000491320000001/Thread_9_0.jpg\r\n'
*get* '150 Binary data connection\n'
*resp* '150 Binary data connection'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
*get* '226 transfer complete\n'
*resp* '226 transfer complete'
Thread_7: count=25
end:Thread_7
Thread_5: count=25
end:Thread_5
Thread_2: count=25
end:Thread_2
Thread_9: count=25
end:Thread_9
Thread_3: count=25
end:Thread_3
Thread_4: count=25
end:Thread_4
Thread_6: count=25
end:Thread_6
Thread_8: count=25
end:Thread_8
Thread_0: count=25
end:Thread_0
Thread_1: count=25
end:Thread_1
['ftp2_threading_1.py']
main exit...
g_count=250
每次更改循环次数有点low,加入命令行参数,代码中的: 93 #for i in range(0,int(sys.argv[1])):
取消注释
在终端输入:python3 ftp_threading.py 1000(循环次数)即可
在终端查看:存入了多少个文件:find . -type f -print|wc -l
python3_ftp多线程上传图片的更多相关文章
- java使用ftp局域网内多线程上传图片过慢
多线程ftp上传文件时候,图片上传很慢,调试和查询资料发现主要在:storeFile方法 解决方案如下: FTPClient fc设置setBufferSize 可以根据内存大小适当设置大点的缓冲区: ...
- Java:网络编程之应用实例
1.聊天 编写一个聊天程序,有收数据的部分和发数据的部分. 这两部分需要同时执行,那么就需要多线程技术. 一个线程控制发送. 一个线程控制接收. 因为收和发动作是不一致的,所以要定义两个run方法,而 ...
- Typora多线程批量上传图片,永久免费25G图床
为了满足日常需求,就写了一个自动上传图片到图床的脚本 运行该程序可以做到自动完成图片上传,并自动替换为网络链接,支持多图同时上传,采用了多线程,上传速度提升很明显. 以Window系统为例,操作步骤: ...
- [Xcode 实际操作]八、网络与多线程-(17)使用网址会话对象URLSession向远程服务器上传图片
目录:[Swift]Xcode实际操作 本文将演示如何通过网址会话对象URLSession向远程服务器上传图片. 网址会话对象URLSession具有在后台上传和下载.暂停和恢复网络操作.丰富的代理模 ...
- -Android -线程池 批量上传图片 -附php接收代码
(出处:http://www.cnblogs.com/linguanh/) 目录: 1,前序 2,类特点 3,用法 4,java代码 5,php代码 1,前序 还是源于重构,看着之前为赶时间写着的碎片 ...
- iOS多线程学习
在 iOS 中其实目前有 4 套多线程方案,他们分别是: Pthreads NSThread GCD NSOperation & NSOperationQueue 所以接下来,我会一一讲解这些 ...
- iOS多线程
关于iOS多线程 概述 这篇文章中,我不会说多线程是什么.线程和进程的区别.多线程有什么用,当然我也不会说什么是串行.什么是并行等问题,这些我们应该都知道的. 在 iOS 中其实目前有 4 套多线程方 ...
- iOS多线程解析
在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项.当然也会给出几种多线程的案例,在实际使用中感受它们的区别.还有一点需要说明的是,这篇文章将会使用 Swift 和 ...
- php多线程抓取信息测试例子
php多线程抓取信息测试例子 PHP 5.3 以上版本,使用pthreads PHP扩展,可以使PHP真正地支持多线程.多线程在处理重复性的循环任务,能够大大缩短程序执行时间. PHP扩展下载:htt ...
随机推荐
- MVC框架图
http://www.cnblogs.com/zgynhqf/archive/2010/11/19/1881449.html MVC框架图 http://www.cnblogs.com/zhang ...
- mac 10.9 dock在多屏幕间移动
想要在哪个屏幕使用dock,就在这个屏幕把鼠标移动到最底部即可.神奇吧?太意外了...居然被我发现了...
- 第十一篇:Linux中权限的再讨论( 下 )
前言 上篇随笔讲述了Linux中权限的大致实现机制以及目录权限的相关规则.本文将讲解Linux中的三种特殊权限:SUID,SGID,Sticky权限. 看完这两篇文章,你一定会对Linux的权限有个更 ...
- 使用 Apache Tiles 3 构建页面布局
参考博客:http://aiilive.blog.51cto.com/1925756/1596059Apache Tiles是一个JavaEE应用的页面布局框架.Tiles框架提供了一种模板机制,可以 ...
- 【BZOJ3831】[Poi2014]Little Bird 单调队列
[BZOJ3831][Poi2014]Little Bird Description In the Byteotian Line Forest there are trees in a row. ...
- LVS负载均衡服务
LVS负载均衡服务 LVS负载均衡调度技术是在Linux内核中实现的,因此被称为Linux虚拟服务器.使用LVS时,不能直接配置内核中的ipvs,而需要使用ipvs的管理工具ipvsadm进行管理. ...
- FTP服务工作原理
1. FTP协议 什么是FTP呢?FTP 是 TCP/IP 协议组中的协议之一,是英文File Transfer Protocol的缩写. 该协议是Internet文件传送的基础,它由一系列规格说明文 ...
- Netty进行RPC服务器的开发 需要考虑的问题
谈谈如何使用Netty开发实现高性能的RPC服务器 - Newland - 博客园 http://www.cnblogs.com/jietang/p/5615681.html 如何实现.基于什么原理? ...
- Linux升级python至3.4.4
wget https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz ls .tgz mkdir /usr/local/python3 cd Py ...
- Django REST framework 理解
Web应用模式 1 .前后端不分离:在前后端不分离的应用模式中,前端页面看到的效果都是由后端控制,由后端渲染页面或重定向,也就是后端需要控制前端的展示,前端与厚度那的耦合度很高. 这种应用模式比较 ...