如何自动通过网络使用python发送文件? [英] How to automate the sending of files over the network using python?

查看:416
本文介绍了如何自动通过网络使用python发送文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我需要做的:

我需要通过网络复制文件。要复制的文件在一台机器上,我需要将其发送到远程机器。它应该是自动的,它应该使用python。我很熟悉os.popen和python的subprocess.Popen。我可以使用这个来复制文件,但是,问题是一次我已经运行one-liner命令(如下所示)

I need to copy files over the network. The files to be copied is in the one machine and I need to send it to the remote machines. It should be automated and it should be made using python. I am quite familiar with os.popen and subprocess.Popen of python. I could use this to copy the files, BUT, the problem is once I have run the one-liner command (like the one shown below)


scp xxx @ localhost:file1.txt yyy @] 192.168.104.XXX:file2.txt

scp xxx@localhost:file1.txt yyy@]192.168.104.XXX:file2.txt

例如


您确定要连接(是/否)吗?

Are you sure you want to connect (yes/no)?

密码:

如果没有弄错,发送此命令(假设我在python中编码)

And if im not mistaken., once I have sent this command (assuming that I code this in python)


conn.modules.os.popen(scp xxx @ localhost:file1。 txt yyy @] 192.168.104.XXX:file2.txt)

conn.modules.os.popen("scp xxx@localhost:file1.txt yyy@]192.168.104.XXX:file2.txt")

,然后是此命令


conn.modules.os.popen(yes)

conn.modules.os.popen("yes")

输出(和我相当肯定,它会给我错误)将是不同的比较它与输出,如果我手动键入它在终端。

The output (and I'm quite sure that it would give me errors) would be the different comparing it to the output if I manually type it in in the terminal.

你知道如何在python中编写代码吗?或者你能告诉我可以解决我的问题的某些(命令等)

Do you know how to code this in python? Or could you tell me something (a command etc.) that would solve my problem

注意:我使用RPyC连接到其他远程计算机, CentOS

Note: I am using RPyC to connect to other remote machines and all machines are running on CentOS

推荐答案

通过Python做正确的方法是使用 fabric

The right way to do it via Python is really using fabric as stated in a comment above.

创建一个名为 fabfile.py的文件

#!/usr/bin/python

from fabric.api import run, env, sudo, put

env.user = 'user'
env.hosts = ['xxxxx.org',]

def copy():
    put('wong_8066.zip', '/home1/user/wong_8066.zip')

然后在本地计算机上,运行<$

Then on your local machine, run fab copy and it will prompt for password.

yeukhon@yeukhon-P5E-VM-DO:~$ fab copy
[xxxxxx.org] Executing task 'copy'
[xxxxxx.org] Login password: 
[xxxxxx.org] put: wong_8066.zip -> /home1/user/wong_8066.zip

Done.

你可以做更多的织物。 Fabric用于部署。
您可以使用fabric在本地运行任务,也可以远程运行多任务主机。

You can do a lot more with fabric. Fabric is used for deployment. You can use fabric to run things locally, or remotely to mutliple host. You can pass many options when u are running.

如果您希望自动化而不使用密码提示,您可以指定文档中所述的ssh-key路径。

If you want to automated this without password prompt, you can specify the path to your ssh-key as stated in the documentation.


fab使用Python的optparse库,意味着它支持典型的
Linux或GNU风格的短和长选项,以及自由地混合
选项和参数。例如。 fab task1 -H主机名task2 -i
路径/到/ keyfile与更简单的fab -H
主机名-i路径/到/ keyfile task1 task2一样有效。

fab uses Python’s optparse library, meaning that it honors typical Linux or GNU style short and long options, as well as freely mixing options and arguments. E.g. fab task1 -H hostname task2 -i path/to/keyfile is just as valid as the more straightforward fab -H hostname -i path/to/keyfile task1 task2.

这篇关于如何自动通过网络使用python发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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