[melbourne-pug] sending stdin to remote python interpreter using fabric

Rasjid Wilcox rasjidw at openminddev.net
Sat Jun 13 15:51:55 CEST 2015


On 12/06/2015 4:45 PM, AMARASINGHAM, Chandra wrote:
>
> I was wondering if anyone had any ideas on how I can send a string of 
> python code to a remote python interpreter using fabric?
>
> I have successfully done this using something like the following, but 
> I don't quite like using the -c flag and would like to send the script 
> as stdin if possible.  The use case is a local Linux server and a 
> remote AIX server both running python 2.7.
>

The main page for python says you can just use - for the filename for 
stdin.  But:

$ python -

seems to go straight to the interactive interpreter.

However

$ cat - | python -

definitely works straight from stdin, as if it was reading a python file.

This all seems like a bit of a hack, but the following fabfile.py seems 
to work for me:

-------

import fabric
from fabric.api import run

pycode = """

# code to be run on remove server(s)

import os
print os.listdir('.')

"""

cmd = """python - << EOF
{pycode}
EOF""".format(pycode=pycode)

def runpycode():
     run(cmd)

-------

Adjust pycode to whatever you want.

Cheers,

Rasjid.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20150613/80ff336e/attachment.html>


More information about the melbourne-pug mailing list