2. \u4ee3\u7801\u5b9e\u73b0<\/strong><\/span><\/div>\nPython\u4ee3\u7801<\/p>\n
#test_upload.py\r\nimport os, sys\r\nimport yaml\r\nfrom optparse import OptionParser\r\nimport paramiko\r\nimport traceback\r\n\r\n\r\nopt_parser = OptionParser()\r\nopt_parser.add_option('--node', action='store', type='string', dest='node', help='sftp config')\r\nopt_parser.add_option('--local_file', action='store', type='string', dest='local_file', help='file to be transferred')\r\n\r\n\r\ndef load_config_from_param_conf(key):\r\n \"\"\"\r\n :param key: \u6307\u5b9a\u4e1a\u52a1\u6240\u4f7f\u7528\u7684sftp\u670d\u52a1\u7684\u540d\u79f0\uff0c\u4f8b\u5982sftp.yaml\u6587\u4ef6\u4e2d\u7684key: test_file_upload\r\n :return: \u4ece\u6307\u5b9a\u8def\u5f84\u62ff\u5230yaml\u6587\u4ef6\u5e76\u8bfb\u53d6\u6307\u5b9akey\uff0c\u8fd4\u56de\u4f8b\u5982sftp.yaml\u6587\u4ef6\u4e2dkey\u4e3atest_file_upload\u4e0b\u7684\u914d\u7f6e\u9009\u9879\u503c\r\n \u5305\u542b: host,port,user,password,destination\r\n \"\"\"\r\n config_file = f'{os.environ[\"TASK_MAIN\"]}\/test_conf\/sftp.yaml' #\u83b7\u53d6\u6307\u5b9a\u8def\u5f84\u4e0b\u7684yaml\u6587\u4ef6\r\n with open(config_file, 'r') as r: #\u6253\u5f00yaml\u6587\u4ef6\u5185\u5bb9\r\n config = yaml.load(r) #\u8f6c\u6362yaml\u6570\u636e\u4e3a\u5b57\u5178\r\n return config['sftp']['upload'][key]\r\n\r\n\r\ndef upload(sftp_conf, local_file_path):\r\n \"\"\"\r\n :param sftp_conf: \u83b7\u53d6\u6307\u5b9asftp_conf=test_file_upload\u4e0b\u8fd4\u56de\u7684\u5b57\u5178\u503c\uff0c\u8fd9\u91cc\u7684\u53c2\u6570sftp_conf\u4e0eload_config_from_param_conf\u51fd\u6570\u91cc\u7684\u53c2\u6570\u503c\u4e00\u81f4\r\n :param local_file_path: \u6307\u5b9a\u672c\u5730\u8981\u4e0a\u4f20\u5230sftp\u670d\u52a1\u5668\u7684\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\r\n :return:\r\n \"\"\"\r\n sf = paramiko.Transport((sftp_conf['host'], sftp_conf['port']))\r\n sf.connect(username=sftp_conf['user'], password=sftp_conf['password'])\r\n sftp = paramiko.SFTPClient.from_transport(sf)\r\n to_path = sftp_conf['destination']\r\n sftp.put(local_file_path, to_path + os.sep + os.path.split(local_file_path)[-1], confirm=False)\r\n sftp.close()\r\n\r\n\r\nif __name__ == '__main__':\r\n option = opt_parser.parse_args(sys.argv[1:])[0]\r\n sftp_conf = load_config_from_param_conf(option.node)\r\n try:\r\n upload(sftp_conf, option.local_file)\r\n except Exception as e:\r\n print(traceback.format_exc())\r\n raise Exception('upload {} file error.'.format(option.local_file))\r\n<\/pre>\nsftp\u914d\u7f6e\u6587\u4ef6\u4ee3\u7801<\/p>\n
#sftp.yaml\r\nsftp:\r\n upload:\r\n test_file_upload:\r\n host: 10.12.34.6\r\n port: 808\r\n user: user_test\r\n password: 8Z.Lx\/2@UH\r\n destination: \/data\/dump\r\n<\/pre>\n3. \u4ee3\u7801\u5206\u6790<\/strong><\/span><\/div>\n(1). yaml \u6a21\u5757<\/p>\n
import yaml\r\n<\/pre>\n\u5bfc\u5165 yaml \u6a21\u5757\u524d\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u6a21\u5757\u7684\u5b89\u88c5<\/p>\n
python3 -m pip install PyYaml\r\n<\/pre>\nyaml \u6a21\u5757\u5728\u8fd9\u91cc\u7684\u4f5c\u7528\u662f\u8bfb\u53d6 sftp \u914d\u7f6e\u6587\u4ef6\u4ee3\u7801\uff0c\u5c06\u6307\u5b9akey: test_file_upload \u4e0b\u7684 key:value \u7684\u503c\u8f6c\u6362\u4e3a\u5b57\u5178\u3002
\n\u4f8b\u5982\uff1aload_config_from_param_conf \u51fd\u6570\u4e2d\u7684\u8fd4\u56de\u503c\u5c31\u662f\u4f7f\u7528 yaml \u8bfb\u53d6\u5230 sftp \u914d\u7f6e\u6587\u4ef6\u4ee3\u7801\u540e\uff0c\u8fd4\u56de key: test_file_upload \u4e0b\u914d\u7f6e\u9009\u9879\u503c\u3002
\n\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n
{ \"host\": \"10.12.34.6\",\r\n \"port\": \"808\",\r\n \"user\": \"user_test\",\r\n \"password\": \"8Z.Lx\/2@UH\",\r\n \"destination\": \"\/data\/dump\"\r\n}\r\n<\/pre>\n\u6700\u540e\u5c06\u8fd4\u56de\u503c\u4f20\u7ed9 upload \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n
(2). OptionParser \u6a21\u5757<\/p>\n
from optparse import OptionParser\r\n<\/pre>\n\u6309\u7167 yaml \u6a21\u5757\u7684\u5b89\u88c5\u65b9\u6cd5\uff0c\u5148\u5b89\u88c5 optparse \u6a21\u5757\u540e\uff0c\u7136\u540e\u5728\u6587\u4ef6\u4e2d\u4eceoptparse \u4e2d\u5bfc\u5165 OptionParser \u6a21\u5757<\/p>\n
opt_parser = OptionParser()\r\nopt_parser.add_option('--node', action='store', type='string', dest='node', help='sftp config')\r\nopt_parser.add_option('--local_file', action='store', type='string', dest='local_file', help='file to be uploaded')\r\n<\/pre>\n\u5728\u8fd9\u91cc\u6211\u4f7f\u7528\u4e86 OptionParser \u8fd9\u4e2a\u7c7b\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff1aopt_parser\uff0c\u901a\u8fc7\u5bf9\u8c61\u6765\u8c03\u7528 add_option \u65b9\u6cd5\u6dfb\u52a0\u4e862\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\uff1anode, local_file<\/p>\n
1). \u5f62\u53c2\uff1a--node\uff0c\u5b9e\u53c2\uff1anode<\/p>\n
\u6240\u4ee3\u8868\u7684\u4e1a\u52a1\u542b\u4e49\u662f\uff1a\u6307\u5b9a\u8981\u4e0a\u4f20\u7684 sftp \u7684\u8282\u70b9\uff0c\u5177\u4f53\u53c2\u6570\u503c\u5bf9\u5e94 sftp\u914d\u7f6e\u6587\u4ef6\u4ee3\u7801\u4e2d\u7684 test_file_upload<\/p>\n
2). \u5f62\u53c2\uff1a--local_file\uff0c\u5b9e\u53c2\uff1alocal_file<\/p>\n
\u6240\u4ee3\u8868\u7684\u4e1a\u52a1\u542b\u4e49\u662f\uff1a\u6307\u5b9a\u672c\u5730\u9700\u8981\u88ab\u4e0a\u4f20\u5230 sftp \u670d\u52a1\u5668\u7684\u5177\u4f53\u6587\u4ef6\u8def\u5f84<\/p>\n
3). \u8c03\u7528\u547d\u4ee4<\/p>\n
python3 test_upload.py --node test_file_upload --local_file \r\n\/home\/test\/sftp_load\/test_001.csv\r\n<\/pre>\n4). add_option()\u65b9\u6cd5<\/p>\n
\r\nadd_option()\u53c2\u6570\u8bf4\u660e\uff1a\r\naction: \u5b58\u50a8\u65b9\u5f0f\uff0c\u5206\u522b\u4e3a:store,store_false,store_true\r\ntype: \u7c7b\u578b\r\ndest: \u5b58\u50a8\u7684\u53d8\u91cf\r\ndefault: \u9ed8\u8ba4\u503c\r\nhelp: \u5e2e\u52a9\u4fe1\u606f\r\n<\/pre>\n\u53c2\u6570\uff1aaction\u7684\u679a\u4e3e<\/p>\n
\r\nstore: \u53c2\u6570\u5217\u8868\u4e2d\u5e26\u6709--node, \u90a3\u4e48\u5c31\u4f1a\u5c06\u4e0b\u4e00\u4e2a\u5143\u7d20\u5373\uff1atest_file_upload \u4f5c\u4e3a\u5176 dest \u5b9e\u53c2 node \u7684\u503c; \u5982\u679c\u6ca1\u6709--node,\u90a3\u4e48\u5bf9\u5e94\u7684node\u7684\u503c\u5c31\u4e3a None;\r\nstore_true: \u53c2\u6570\u5217\u8868\u4e2d\u6709--local_file, \u90a3\u4e48\u5176 dest \u5b9e\u53c2 local_file \u7684\u503c\u5c31\u4e3a True; \u5426\u8005\u5c31\u4e3a default \u5b9a\u4e49\u7684\u9ed8\u8ba4\u503c\uff0c\u8fd9\u91cc\u6ca1\u6709\u7ed9\u5b9a default \u7684\u9ed8\u8ba4\u503c;\r\nstore_false: \u53c2\u6570\u5217\u8868\u4e2d\u6709--local_file, \u90a3\u4e48\u5176 dest \u5b9e\u53c2 local_file \u7684\u503c\u5c31\u4e3a False; \u5426\u8005\u5c31\u4e3a default \u5b9a\u4e49\u7684\u9ed8\u8ba4\u503c\uff0c\u8fd9\u91cc\u6ca1\u6709\u7ed9\u5b9a default \u7684\u9ed8\u8ba4\u503c;\r\n<\/pre>\n\u53c2\u6570\uff1atype<\/p>\n
\r\ntype\u662f\u6307\u5b9a\u4f20\u5165\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u8fd9\u91cc\u7684\u53c2\u6570\u7c7b\u578b\u4e3a string \u7c7b\u578b\u3002<\/pre>\n\u53c2\u6570\uff1adest<\/p>\n
\r\ndest\u662f\u53c2\u6570\u4f20\u5165\u540e\u7531\u54ea\u4e2a\u53d8\u91cf\u6765\u5b58\u50a8\u7684\uff0c\u540e\u9762\u4ee3\u7801\u5bf9\u8be5\u53c2\u6570\u7684\u5f15\u7528\u4e5f\u662f\u4f7f\u7528\u8fd9\u91cc\u5b9a\u4e49\u7684\u53d8\u91cf\u540d\u6765\u5f15\u7528\u7684\u3002<\/pre>\n\u53c2\u6570\uff1adefault<\/p>\n
\r\ndefault \u662f\u4e0e action \u7684\u503c\u7ed3\u5408\u4f7f\u7528\u7684\u3002\r\n\u5f53action=store_true\u65f6\uff0cdefault=123\u65f6\uff0c\u5982\u679c\u6709\u4f20\u5165\u5f62\u53c2--local_file\uff0c\u5219\u5b9e\u53c2local_file\u7684\u503c\u4e3a\uff1aTrue\r\n\u5f53action=store_true\u65f6\uff0cdefault=123\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u4f20\u5165\u5f62\u53c2--local_file\uff0c\u5219\u5b9e\u53c2local_file\u7684\u503c\u4e3a\uff1a123\r\n\u5f53action=store_false\u65f6\uff0cdefault=123\u65f6\uff0c\u5982\u679c\u6709\u4f20\u5165\u5f62\u53c2--local_file\uff0c\u5219\u5b9e\u53c2local_file\u7684\u503c\u4e3a\uff1aFalse\r\n\u5f53action=store_false\u65f6\uff0cdefault=123\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u4f20\u5165\u5f62\u53c2--local_file\uff0c\u5219\u5b9e\u53c2local_file\u7684\u503c\u4e3a\uff1a123<\/pre>\n\u53c2\u6570\uff1ahelp<\/p>\n
\r\nhelp\u76f8\u5f53\u4e8e\u5e2e\u52a9\u8bf4\u660e\u6587\u6863\uff0c\u7528\u4e8e\u63cf\u8ff0\u8fd9\u4e2a\u53c2\u6570\u7684\u542b\u4e49\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"\u6700\u8fd1\u63a5\u5230\u4e00\u4e2a\u4ea7\u54c1\u9700\u6c42\u662f\u7ed9\u6307\u5b9a\u7684 sftp \u670d\u52a1\u5668\u7684\u6307\u5b9a\u76ee\u5f55\u5b9a\u65f6\u63a8\u9001\u6587\u4ef6\u6570\u636e\u3002 \u56e0\u4e3a\u9879\u76ee\u7ec4\u5df2\u6709\u73b0\u6210\u7684\u7ec4\u4ef6\u53ef\u4ee5\u8f7b\u677e […]<\/p>\n","protected":false},"author":643,"featured_media":255273,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-255245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/255245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=255245"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/255245\/revisions"}],"predecessor-version":[{"id":255272,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/255245\/revisions\/255272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/255273"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=255245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=255245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=255245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}