Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 878333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:45:16+00:00 2026-05-15T11:45:16+00:00

I’m trying to write a simple custom storage system. I need only that my

  • 0

I’m trying to write a simple custom storage system.

I need only that my storage system duplicate files from a few FTP servers.

Subclass of storage looks like:

from django.core.files.storage import Storage
from settings import MEDIA_ROOT, MEDIA_URL
import ftplib
import os



class FTPStorage(Storage):
    """FTP Storage class for Django pluggable storage system."""

    def __init__(self, mediaroot=MEDIA_ROOT, base_url=MEDIA_URL):
        self._netrc=open('netrc')
        self._machine=[]
        self._login=[]
        self._password=[]


 def _save(self, name, content):
     content.open()
        if hasattr(content, 'chunks'):
            content_str = ''.join(chunk for chunk in content.chunks())
        else:
            content_str = content.read() 
        for line in netrc: #read netrc file
            old=line.strip()
            line=line.strip().split()
            if old.startswith("machine"): machine.append(line[-1])
            if old.startswith("login"): login.append(line[-1])
            if old.startswith("password"): password.append(line[-1])
        for i in range(len(machine)):
            try:
               ftp = ftplib.FTP(machine[i])
               ftp.login(login[i],password[i])
            except Exception,e:
                print e
            else:
                ftp.cwd("PublicFolder")
                ftp.storbinary("STOR " + os.path.basename(name), StringIO(content_str), "rb", 8*1024)  
        return name

But now it give me the error:

line:20, in __init__
if hasattr(content, ‘chunks’):
Name error: Global name is not defined.

Can I just write the _save method, so it’ll work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:45:17+00:00Added an answer on May 15, 2026 at 11:45 am

    Your problem has more to do with the format of your code, and not the logic of it. That is, you have some syntax errors.

    The error message you included states that the if hasattr(content, 'chunks'): line occurs in the __init__ method, which suggests that the interpreter believes you to be defining the _save method as a function within the init method’s scope. Your indentation, while not entirely clear here on StackOverflow, seems messy and may be the cause of this confusion. Python is very sensitive to indentation errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.