使用Python传输文件



我必须使用Python将文件从远程计算机传输到Amazon EC2,然后从那里转移到Amazon S3。我已经成功上传了一个文本文件,但不知道如何上传多个文件。这是一个文件的两个程序的代码。

将文件获取到EC2

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

将文件上传到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

循环从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()

使用相同的原理从 list mylist = [" a"," b"," c"]
https://wiki.python.org/moin/forloop

注意:
如果在不同的URL中使用了相同的文件名,则结果将被覆盖,因此您需要添加测试或其他某些应对机制,如果是概率。
我已经使用os.path.split()获取文件名,您可能会发现urlparse功能为您提供了更好的里程,而URL更复杂

这是您可以在每台计算机上安装python的方式传输文件的方式。

首先安装Pyformulas pip install pyformulas==0.2.7

然后运行服务器(接收文件):

服务器:

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)]

然后客户端(发送文件):

客户端:

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) ]

最新更新