Pass dict as an argument over SSH to Python script

大兔子大兔子 提交于 2020-12-25 18:58:52

问题


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')

I am getting an error:

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

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

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

Any suggestions?


回答1:


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')

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]))


来源:https://stackoverflow.com/questions/63866638/pass-dict-as-an-argument-over-ssh-to-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!