简体   繁体   English

在本地机器和远程服务器之间传输文件

[英]transfer files between local machine and remote server

I want to make access from remote ubuntu server to local machine because I have multiple files in this machine and I want to transfer it periodically (every minute) to server how can I do that using python我想从远程 ubuntu 服务器访问本地机器,因为我在这台机器上有多个文件,我想定期(每分钟)将它传输到服务器我怎么能使用 python 做到这一点

Depending on your local machine OS and network setup, I would recommend the following: 根据您本地计算机的操作系统和网络设置,我建议以下内容:

File transfers 文件传输

Based on the file size, if its a small copy, I would use scp (secure copy). 根据文件大小,如果它是小副本,我将使用scp (安全副本)。 This is because of the simplicity of the command. 这是因为命令简单。

In most use cases however I would use rsync because of its great capabilities, most importantly the ability to handle failed partial transfers. 但是,在大多数情况下,我会使用rsync因为它具有强大的功能,最重要的是能够处理失败的部分传输。 It works by analysing the differences between the source and destination. 它通过分析源和目标之间的差异来工作。 It has pretty much every preference under the sun available (overwriting, deltas, etc.) 在阳光下,它几乎具有所有偏好(覆盖,增量等)。

Note that when using these commands in an automation script over a longer period of time, you'll probably want to set up a static IP or DDNS for your remote machine. 请注意,在较长时间的自动化脚本中使用这些命令时,可能要为远程计算机设置静态IP或DDNS。

Python 蟒蛇

To run shell commands in a Python script, use pexpect . 要在Python脚本中运行shell命令,请使用pexpect Its built around the original C based expect and it's fantastic. 它围绕最初的基于C的expect构建,这太棒了。 I used it the other day to transfer a folder from a dev computer to a number of different Raspberry Pis remotely at the same time. 前几天,我用它同时将文件夹从开发计算机远程传输到许多不同的Raspberry Pi。 Check out the documentation here: https://pexpect.readthedocs.io/en/stable/ 在此处查看文档: https : //pexpect.readthedocs.io/en/stable/

Automation 自动化

As for automation, it really depends on how you want it set up. 至于自动化,这实际上取决于您要如何设置。 If you want the python script responsible for transferring data to be called when you want to transfer data you could look into crontab . 如果要在传输数据时调用负责传输数据的python脚本,可以查看crontab It's very well known by admins so easy to Google. 管理员非常了解Google,因此非常容易。

Alternatively, if this is part of a Python app you could have the app running in the background and sleeping ( time.sleep() or a time elapsed function to check) between transfers. 或者,如果这是Python应用程序的一部分,则可以让该应用程序在后台运行并在两次传输之间处于睡眠状态( time.sleep()或要检查时间的函数)。 If you needed to do other things in the same Python app then you could stick the whole transfer and sleep part into a thread (also easily implemented in Python). 如果您需要在同一个Python应用程序中执行其他操作,则可以将整个传输和睡眠部分粘贴到线程中(也可以在Python中轻松实现)。

I hope this helps, let me know if you want details elaborated. 希望对您有所帮助,如果您需要详细说明,请告诉我。

You can easily transfer files between local and remote or between two remote servers.您可以轻松地在本地和远程之间或两个远程服务器之间传输文件。 If both servers are Linux-based and require to transfer multiple files and folder using single command, however, you need to follow up below steps:但是,如果两台服务器都是基于 Linux 的,并且需要使用单个命令传输多个文件和文件夹,则需要执行以下步骤:

  • User from one remote server should have access to another remote server to corresponding directory you want to transfer the file.来自一个远程服务器的用户应该可以访问另一个远程服务器到您要传输文件的相应目录。

  • You might need to create a policy or group and assign to server list to that group which you want to access and assign the user to that group so 2 different remote server can talk to each other.您可能需要创建策略或组并将服务器列表分配给您要访问的组,并将用户分配给该组,以便 2 个不同的远程服务器可以相互通信。

  • Run the following scp command:-运行以下 scp 命令:-

     scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2 scp [选项] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python将文件从本地计算机传输到远程Windows服务器 - Transfer file from local machine to remote windows server using python 如何使用python中的第三台本地计算机使用scp在两个远程服务器之间传输文件 - How to transfer a file between two remote servers using scp from a third, local machine in python 使用 Python 将本地服务器上一个目录中的多个文件传输到远程服务器上的不同目录 - Transfer multiple files in a directory on a local server to different directories on remote server in Python 如何将文件从远程Windows服务器获取到本地Windows计算机目录? - How to get files from Remote windows server to local Windows machine directory? 使用python将本地文件传输到远程服务器(窗口) - Transfer a local file to a remote server(windows) using python 如何使用paramiko在两个远程服务器之间传输文件? - How to use paramiko to transfer files between two remote servers? 是否可以从本地计算机访问(而不是复制)远程文件? - Is it possibile to access ( not copy ) files in my remote from my local machine? 在存储在远程服务器上的本地计算机上运行 python 脚本 - run python script on local machine stored on remote server Python PySFTP将文件从一台远程服务器传输到另一台远程服务器 - Python PySFTP transfer files from one remote server to another remote server 远程机器上的 Bottle 服务器 - Bottle server on remote machine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM