Python FTP

This is the place for queries that don't fit in any of the other categories.

Python FTP

Postby timbo_cole » Fri May 17, 2013 8:15 pm

Hi there,
I am new to Python but I'm making a script for an FTP distribution server but I am stuck on one 1 thing. The whole thing is working 100% but the way in which it distributes means that if a server is offline (eg. the 3rd one) then any server that is online but below that in the list, in this case the fourth server would not recieve the file. Is there any solution to this issue?
Code: Select all
class MyHandler(FTPHandler):
    def on_file_received(self, file):
        file_name = os.path.basename(file)
   ftp = FTP('hostname')
   ftp.login('Username', 'Password')
   ftp.storbinary('STOR ' + file_name, open(file, 'rb'))
   ftp = FTP('hostname')
   ftp.login('Username', 'Password')
   ftp.storbinary('STOR ' + file_name, open(file, 'rb'))
        ftp = FTP('hostname')
   ftp.login('Username', 'Password')
   ftp.storbinary('STOR ' + file_name, open(file, 'rb'))
   ftp = FTP('hostname')
   ftp.login('Username', 'Password')
   ftp.storbinary('STOR ' + file_name, open(file, 'rb'))
   os.remove(file)
Last edited by stranac on Sat May 18, 2013 2:39 pm, edited 1 time in total.
Reason: Included the code in post, instead of a paste link
timbo_cole
 
Posts: 9
Joined: Fri May 17, 2013 8:12 pm

Re: Python FTP

Postby timbo_cole » Sat May 18, 2013 7:28 am

This has been solved, with thanks to: Confab, jonesht-laptop and pydesigner on the IRC channel :)
timbo_cole
 
Posts: 9
Joined: Fri May 17, 2013 8:12 pm

Re: Python FTP

Postby stranac » Sat May 18, 2013 2:37 pm

timbo_cole wrote:This has been solved, with thanks to: Confab, jonesht-laptop and pydesigner on the IRC channel :)


You should paste the final code here, to let others know how your problem was solved.
I hear confab has shown you some neat code.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python FTP

Postby timbo_cole » Sat May 18, 2013 4:01 pm

Yeah, he showed me dictionarys, please find the code below:

Code: Select all
File = #INSERT FILE NAME HERE
        server_info = {'hostname': ('username', 'password'),
          ''.join(list('hostname')): ('username', 'password'),
          ''.join(list('hostname')): ('username', 'password'),
                       'hostname': ('username', 'password')}
        for server, info in server_info.iteritems():
            try:
                ftp = FTP(server)
                ftp.login(*info)
                ftp.storbinary('STOR ' + file_name, open(file, 'rb'))
            except:
                print server
                continue
        os.remove(file)
timbo_cole
 
Posts: 9
Joined: Fri May 17, 2013 8:12 pm

Re: Python FTP

Postby micseydel » Sun May 19, 2013 2:50 am

What in the world is that? Really, what's going on with the server_info assignment? Do you realize that there's only really one entry in that dictionary? Also, I would highly recommend you catch only the specific exception here rather than a blanket one, or even if you want to catch absolutely everything, noting it somehow.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python FTP

Postby stranac » Sun May 19, 2013 7:19 pm

micseydel wrote:What in the world is that? Really, what's going on with the server_info assignment? Do you realize that there's only really one entry in that dictionary? Also, I would highly recommend you catch only the specific exception here rather than a blanket one, or even if you want to catch absolutely everything, noting it somehow.


'hostname' and ('username', 'password') were only used there as placeholders.
He changed those values in his real code.

And just for the fun of it, here's the most complete version(still can't run like this) of the code that was provided:
Code: Select all
import random
 
def decorated(f):
    items = [f] * random.randint(1, 20)
    return next(item for item in items)
 
Bong = list
 
 
class MyHandler(FTPHandler):
    @decorated
    def on_file_received(self, file):
        file_name = os.path.basename(file)
        # Keep a dict with the info of each server.
        server_info = {''.join(Bong('192.168.1.xx')): ('username', 'password'),
                       ''.join(Bong('192.168.1.xx')): ('username', 'password'),
                       ''.join(Bong('192.168.1.xx')): ('username', 'password'),
                       ''.join(Bong('192.168.xx.lastquad')): ('username', 'password')}
 
        for server, info in server_info.iteritems():
            try:
                ftp = FTP(server)
                ftp.login(*info)
                ftp.storbinary('STOR ' + file_name, open(file, 'rb'))
            except: # give it an explicit exception. What is this?
                print server
                continue
        os.remove(file)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python FTP

Postby micseydel » Sun May 19, 2013 10:27 pm

Code: Select all
>>> Bong = list
>>> server_info = {''.join(Bong('192.168.1.xx')): ('username', 'password'),
                       ''.join(Bong('192.168.1.xx')): ('username', 'password'),
                       ''.join(Bong('192.168.1.xx')): ('username', 'password'),
                       ''.join(Bong('192.168.xx.lastquad')): ('username', 'password')}
>>> server_info
{'192.168.1.xx': ('username', 'password'), '192.168.xx.lastquad': ('username', 'password')}


There's a great deal more code there than necessary, even if Bong is later supposed to be a class that produced all the different possibilities for that IP or something.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 5 guests