简体   繁体   English

使用python传输文件

[英]transfer files using python

I have to transfer files from my remote computer to amazon ec2 and then from there to Amazon s3 using python . 我必须使用python将文件从远程计算机传输到Amazon ec2,然后从那里传输到Amazon s3。 I have successfully uploaded one text file but dont know how to upload multiple files. 我已经成功上传了一个文本文件,但不知道如何上传多个文件。 Here are the codes for two programs for one file. 这是一个文件的两个程序的代码。

to get file to ec2 将文件获取到ec2

import urllib
source = urllib.urlopen('url').read()
fhand = open('file2.txt','w')
fhand.write(source)
fhand.close()

to upload file to s3 上传文件到s3

import boto
from boto.s3.key import Key
keyId = "acess key"
skeyId = "secret key"

fileName="file2.txt"
bucketName="bname"
file=open(fileName)

conn = boto.connect_s3(keyId,skeyId)
print conn
bucket = conn.get_bucket(bucketName)
print bucket
k = Key(bucket)
print k
k.key=fileName
print k.key
result = k.set_contents_from_file(file)
print result

Looping to get files from ec2 循环从ec2获取文件

import urllib,os
sources = ['//url1/file1.txt','//url2/file2.txt','//url3/file3.txt'] # List of urls
for myurl in sources:
    source = urllib.urlopen(myurl).read() #Open each url file
    fname = os.path.split(myurl)[1]       #Split the url to get the file name 
    print('Copying file:',fname)
    fhand = open(fname,'w')
    fhand.write(source)
    fhand.close()

Use the same principle to send files from a list mylist=["a","b","c"] 使用相同的原理从list mylist = [“ a”,“ b”,“ c”]发送文件
https://wiki.python.org/moin/ForLoop https://wiki.python.org/moin/ForLoop

Notes: 笔记:
If the same file name is used in different urls, the results will be overwritten, so you will want to add a test or some other coping mechanism, if that is a probability. 如果在不同的url中使用了相同的文件名,则结果将被覆盖,因此,如果有可能,您将需要添加测试或其他应对机制。
I have used os.path.split() to get the file name, you may find that the urlparse function gives you better mileage, with more complicated urls 我已经使用os.path.split()来获取文件名,您可能会发现urlparse函数为您提供了更好的性能,并带有更复杂的url

Here is how you can transfer files with pyformulas , if python is installed on each machine. 如果在每台机器上都安装了python,则可以使用pyformulas传输文件。

First install pyformulas pip install pyformulas==0.2.7 首先安装pyformulas pip install pyformulas==0.2.7

Then run the server (receives the files): 然后运行服务器(接收文件):

Server: 服务器:

import pyformulas as pf

class server(pf.net.Stream):
    file = bytes()
    def on_receive(self, conn, buffer):
        self.file += buffer

    def on_disconnect(self, conn):
        with open('file_%i' % self.port, 'wb') as file:
            file.write(self.file)

servers = [server(port=1000+i) for i in range(3)]

Then the client (sends the files): 然后客户端(发送文件):

Client: 客户:

import pyformulas as pf

class send_file(pf.net.Stream):
    def __init__(self, port, file, address):
        self.file = file
        super(send_file, self).__init__(port, address)

    def on_connect(self, conn):
        conn.send(self.file)
        conn.disconnect()

filenames = ['0.txt', '1.wav', '2.bin'] # Put the filenames here

[ send_file(port=1000+idx, file=open(fname, 'rb').read(), address='server ip address') for idx, fname in enumerate(filenames) ]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM