问题
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