Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the content of a remote file without a local temporary file with fabric

Tags:

python

fabric

I want to get the content of a remote file with fabric, without creating a temporary file.

like image 653
guettli Avatar asked Oct 11 '13 13:10

guettli


People also ask

How do you answer prompts automatically with Python fabric?

The section about Prompts in the Fabric documentation says: The prompts dictionary allows users to control interactive prompts. If a key in the dictionary is found in a command's standard output stream, Fabric will automatically answer with the corresponding dictionary value.

What is the fabric in Python and what is its usage?

Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric is very simple and powerful and can help to automate repetitive command-line tasks. This approach can save time by automating your entire workflow.

Is fabric an open source?

Headless e-commerce platforms like fabric fuse the customization and versatility of open source e-commerce solutions with the benefits and ongoing support of SaaS alternatives, helping brands scale and grow without incurring significant technical debt.


2 Answers

from StringIO import StringIO
from fabric.api import get

fd = StringIO()
get(remote_path, fd)
content=fd.getvalue()
like image 152
guettli Avatar answered Sep 21 '22 19:09

guettli


With Python 3 (and fabric3), I get this fatal error when using io.StringIO: string argument expected, got 'bytes', apparently because Paramiko writes to the file-like object with bytes. So I switched to using io.BytesIO and it works:

from io import BytesIO

def _read_file(file_path, encoding='utf-8'):
    io_obj = BytesIO()
    get(file_path, io_obj)
    return io_obj.getvalue().decode(encoding)
like image 20
Carl G Avatar answered Sep 23 '22 19:09

Carl G