将 dict 作为参数通过 SSH 传递给 Python 脚本 [英] Pass dict as an argument over SSH to Python script

查看:67
本文介绍了将 dict 作为参数通过 SSH 传递给 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 os 模块使用 ssh 命令传递 dict 参数:

I am trying to pass dict arguments using the ssh command via the os module:

os.system(f'ssh remote_host python -u - {dict1} {dict2} < local_script.py')

我收到一个错误:

sh:line:0 意外标记附近的语法错误 ('

sh:line:0 syntax error near unexpected token ('

将 dict 作为参数传递的正确语法是什么?

What is the right syntax to pass dict as an argument?

如果我传递字符串而不是字典,它就可以正常工作.

If I pass string instead of dict, it works fine.

有什么建议吗?

推荐答案

使用 json 和 urlencode.

Use json and urlencode.

import urllib.parse
import json
dict1_j = urllib.parse.quote(json.dumps(dict1))
dict2_j = urllib.parse.quote(json.dumps(dict2))
os.system(f'ssh remote_host python -u - {dict1_j} {dict2_j} < local_script.py')

你可以在local_script.py中使用urldecode和json pharse来解码

And you can use urldecode and json pharse to decode this in local_script.py

import json 
import urllib.parse 
dict1 = json.loads(urllib.parse.unquote(sys.argv[1])) 
dict2 = json.loads(urllib.parse.unquote(sys.argv[2]))

这篇关于将 dict 作为参数通过 SSH 传递给 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆