transfer/client.py

61 lines
2.0 KiB
Python
Raw Normal View History

2016-12-16 14:59:07 +08:00
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
2016-12-19 14:11:17 +08:00
from tornado.simple_httpclient import SimpleAsyncHTTPClient
2016-12-16 14:59:07 +08:00
import tornado.ioloop
import tornado.web
import os,sys
from tornado import gen
from functools import partial
2016-12-19 14:11:17 +08:00
#import time,datatime
2016-12-16 14:59:07 +08:00
total_downloaded = 0
#action = sys.argv[1]
#filepath = sys.argv[2]
#uid = sys.argv[3]
#gid = sys.argv[4]
#pos = sys.argv[5]
2016-12-19 14:11:17 +08:00
def geturl(action,host,filepath,uid,gid,pos,size):
2016-12-16 14:59:07 +08:00
if action=="read":
# uid = sys.argv[3]
# gid = sys.argv[4]
# pos = sys.argv[5]
2016-12-27 17:24:32 +08:00
url = "http://"+host+"/read?filepath="+filepath+"&uid="+uid+"&gid="+gid+"&pos="+pos+"&size="+size
2016-12-16 14:59:07 +08:00
print(url)
return url
def chunky(path, chunk):
# print("self._max_body_size",self._max_body_size)
global total_downloaded
total_downloaded += len(chunk)
2016-12-27 17:24:32 +08:00
# print("chunk size",len(chunk))
2016-12-16 14:59:07 +08:00
# the OS blocks on file reads + writes -- beware how big the chunks is as it could effect things
2016-12-19 14:11:17 +08:00
with open(path, 'ab') as f:
2016-12-16 14:59:07 +08:00
f.write(chunk)
@gen.coroutine
2016-12-19 14:11:17 +08:00
def writer(action,host,filepath,targetdir,uid,gid,pos,size):
2016-12-16 14:59:07 +08:00
print("writer function")
# tornado.ioloop.IOLoop.instance().start()
2016-12-19 14:11:17 +08:00
file_name = targetdir+os.path.basename(filepath)
if os.path.exists(targetdir):
pass
else:
2016-12-27 17:24:32 +08:00
os.makedirs(targetdir)
2016-12-19 14:11:17 +08:00
f = open(file_name,'w')
2016-12-16 14:59:07 +08:00
f.close()
2016-12-19 14:11:17 +08:00
request = HTTPRequest(geturl(action,host,filepath,uid,gid,pos,size), streaming_callback=partial(chunky, file_name))
AsyncHTTPClient.configure('tornado.simple_httpclient.SimpleAsyncHTTPClient', max_body_size=512*1024*1024)
http_client = AsyncHTTPClient(force_instance=True)
#http_client.configure("tornado.simple_httpclient.SimpleAsyncHTTPClient",max_body_size=524288000)
2016-12-16 14:59:07 +08:00
response = yield http_client.fetch(request)
tornado.ioloop.IOLoop.instance().stop()
print("total bytes downloaded was", total_downloaded)
2016-12-19 14:11:17 +08:00
def entrance(action,host,filepath,targetdir,uid,gid,pos,size):
#print("entrance function")
writer(action,host,filepath,targetdir,uid,gid,pos,size)
2016-12-16 14:59:07 +08:00
tornado.ioloop.IOLoop.instance().start()