Unknown errors, RSS feed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhass
    New Member
    • Dec 2008
    • 11

    Unknown errors, RSS feed

    From my other post I am making a simple program that creates an RSS feed with Python. Now when I run my program so far, I get errors. It says "something is not defined". The word something is replaced by the name of the function I'm trying to use. my article function when ran, comes up with "article is not defined" etc.

    Here's my code: What is wrong with it?

    Code:
    #!/usr/bin/env python
    import ftplib
    import getpass
    
    def mainfeed():
        name=raw_input("Name of Feed: ")
        desc=raw_input("Description of Feed: ")
        url=raw_input("Location of Website: ")
        again()
    
    def again():
        again=raw_input("Would you like to add an article to the feed? Y/N: ")
        if again == "Y":
            article()
        elif again =="N":
            writeb()
        else:
            print again + """is not a valid option. You must select either 'Y' or 'N'"""
            fail()
    
    def fail():
        again()
    
    def article():
        aname=raw_input("Name of Article: ")
        adesc=raw_input("Description of Article: ")
        aurl=raw_input("Location of Article: ")
        finish()
    
    def finish():
        print "Saving parameters to rss-feed.xml...please wait..."
        print "Finished"   
        writea()
    
    def writea():
        file_name = "rss-feed.xml"
        f = open(file_name, 'w')
        outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
        outlist.append('  <channel>\n    <title>%s</title>' % name)
        outlist.append('    <link>%s</link>' % url)
        outlist.append('    <description>%s</description>' % desc)
        outlist.append('    <item>\n        <title>%s</title>' % aname)
        outlist.append('        <link>%s</link>' % aurl)
        outlist.append('        <description>%s</description>' % adesc)
        outlist.append('    </item>')
        outlist.append('  </channel>')
        outlist.append('</rss>')  
        f.write('\n'.join(outlist))
        f.close()
        ftpqe()
    
    def writeb():
        file_name = "rss-feed.xml"
        f = open(file_name, 'w')
        outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
        outlist.append('  <channel>\n    <title>%s</title>' % name)
        outlist.append('    <link>%s</link>' % url)
        outlist.append('    <description>%s</description>' % desc)
        outlist.append('  </channel>')
        outlist.append('</rss>')  
        f.write('\n'.join(outlist))
        f.close()
        ftpqe()
    
    def ftpqe():
        ftpq=raw_input("Do you want to upload your XML file to your website via FTP? Y/N: ")
        if ftpq == "Y":
            ftp()
        elif ftpq =="N":
            end()
        else:
            print again + """is not a valid option. You must select either 'Y' or 'N'"""
            fail2()
    
    def fail2():
        ftpqe()
    
    def ftp():
        server=raw_input("Server name: ")
        login=raw_input("Enter your login: ")
        password = getpass.unix_getpass("Enter your password: ")
        ftplib.FTP(server,login,password)
        f = open('rss-feed.xml','rb') 
        s.storbinary('STOR rss-feed.xml', f)
        f.close()
        s.quit()
        print "Your file has been uploaded"
        end()
    
    def end():
        print "Thank you"
    
    mainfeed()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    In this line:[code=Python]s.storbinary('S TOR rss-feed.xml', f)[/code]I don't see where s is defined. It would be helpful if you would post the exact error message and include all tracebacks. If you are not seeing this information, try enclosing your code in a try/except block similiar to this:
    [code=Python]import sys, traceback

    def formatException Info(level=6):
    error_type, error_value, trbk = sys.exc_info()
    info_list = ["Error: %s \nDescription: %s \nTraceback:\n" % (error_type.__n ame__, error_value), ]
    info_list.exten d(traceback.for mat_tb(trbk, level))
    return ''.join(info_li st)

    try:
    mainfeed()
    except:
    print formatException Info()[/code]

    Comment

    • bhass
      New Member
      • Dec 2008
      • 11

      #3
      Here are the errors I get so far:

      No 1:
      Code:
      Name of Feed: test
      Description of Feed: test
      Location of Website: test
      Would you like to add an article to the feed? Y/N: N
      Traceback (most recent call last):
        File "rssgps.py", line 93, in <module>
          mainfeed()
        File "rssgps.py", line 9, in mainfeed
          again()
        File "rssgps.py", line 16, in again
          writeb()
        File "rssgps.py", line 56, in writeb
          outlist.append('  <channel>\n    <title>%s</title>' % name)
      NameError: global name 'name' is not defined
      no 2
      Code:
      Name of Feed: test
      Description of Feed: test
      Location of Website: test
      Would you like to add an article to the feed? Y/N: Y
      Name of Article: test
      Description of Article: test
      Location of Article: test
      Saving parameters to rss-feed.xml...please wait...
      Finished
      Traceback (most recent call last):
        File "rssgps.py", line 93, in <module>
          mainfeed()
        File "rssgps.py", line 9, in mainfeed
          again()
        File "rssgps.py", line 14, in again
          article()
        File "rssgps.py", line 28, in article
          finish()
        File "rssgps.py", line 33, in finish
          writea()
        File "rssgps.py", line 39, in writea
          outlist.append('  <channel>\n    <title>%s</title>' % name)
      NameError: global name 'name' is not defined
      I have more on the functions that are executed later on in the script, but this is so far.

      But name IS defined, from the raw input at the start of the mainfeed function!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        It is a matter of scope and the way Python resolves names. When a function executes, a new namespace is created which includes the names of the function parameters and variables assigned in the function. When resolving names, the interpreter first checks the local namespace, in this case, the namespace created by the function. If no match is found, it checks the global namespace. The global namespace is always the module which defines the function. If no match is found, it checks the built-in namespace before raising a NameError exception. As you can see, the interpreter never checks the namespaces of the other functions in your module. You must make the variable names global or pass arguments to your functions.

        Comment

        • bhass
          New Member
          • Dec 2008
          • 11

          #5
          Originally posted by bvdet
          It is a matter of scope and the way Python resolves names. When a function executes, a new namespace is created which includes the names of the function parameters and variables assigned in the function. When resolving names, the interpreter first checks the local namespace, in this case, the namespace created by the function. If no match is found, it checks the global namespace. The global namespace is always the module which defines the function. If no match is found, it checks the built-in namespace before raising a NameError exception. As you can see, the interpreter never checks the namespaces of the other functions in your module. You must make the variable names global or pass arguments to your functions.
          Thanks! I'll make them global now, and then I'll post the other errors later. That's one bit solved.

          Comment

          • bhass
            New Member
            • Dec 2008
            • 11

            #6
            Code:
            #!/usr/bin/env python
            import ftplib
            import getpass
            
            name=raw_input("Name of Feed: ")
            desc=raw_input("Description of Feed: ")
            url=raw_input("Location of Website: ")
            print "Your main feeds settings have been updated"
            print "Now information about adding an article to the feed.Other articles can be added manually later"
            aname=raw_input("Name of Article: ")
            adesc=raw_input("Description of Article: ")
            aurl=raw_input("Location of Article: ")
            finish()
            
            def finish():
                print "Saving parameters to rss-feed.xml...please wait..."
                print "Finished"   
                writea()
            
            def writea():
                file_name = "rss-feed.xml"
                f = open(file_name, 'w')
                outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
                outlist.append('  <channel>\n    <title>%s</title>' % name)
                outlist.append('    <link>%s</link>' % url)
                outlist.append('    <description>%s</description>' % desc)
                outlist.append('    <item>\n        <title>%s</title>' % aname)
                outlist.append('        <link>%s</link>' % aurl)
                outlist.append('        <description>%s</description>' % adesc)
                outlist.append('    </item>')
                outlist.append('  </channel>')
                outlist.append('</rss>')  
                f.write('\n'.join(outlist))
                f.close()
                ftpqe()
            
            def writeb():
                file_name = "rss-feed.xml"
                f = open(file_name, 'w')
                outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
                outlist.append('  <channel>\n    <title>%s</title>' % name)
                outlist.append('    <link>%s</link>' % url)
                outlist.append('    <description>%s</description>' % desc)
                outlist.append('  </channel>')
                outlist.append('</rss>')  
                f.write('\n'.join(outlist))
                f.close()
                ftpqe()
            
            def ftpqe():
                ftpq=raw_input("Do you want to upload your XML file to your website via FTP? Y/N: ")
                if ftpq == "Y":
                    ftp()
                elif ftpq =="N":
                    end()
                else:
                    print again + """is not a valid option. You must select either 'Y' or 'N'"""
                    fail2()
            
            def fail2():
                ftpqe()
            
            def ftp():
                server=raw_input("Server name: ")
                login=raw_input("Enter your login: ")
                password = getpass.unix_getpass("Enter your password: ")
                ftplib.FTP(server,login,password)
                f = open('rss-feed.xml','rb') 
                s.storbinary('STOR rss-feed.xml', f)
                f.close()
                s.quit()
                print "Your file has been uploaded"
                end()
            
            def end():
                print "Thank you for using "
            I changed it to this now, the names and article names have been made global, and now I get this error. It's this I don't understand the most, because finish is obviously a function. error:

            Code:
            Traceback (most recent call last):
              File "rssgps.py", line 13, in <module>
                finish()
            NameError: name 'finish' is not defined
            Do I misunderstand functions?

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              You have to define it before you call it.
              [code=Python]
              def finish():
              ....do stuff....

              finish()[/code]

              Comment

              • bhass
                New Member
                • Dec 2008
                • 11

                #8
                Thanks once again, you've really helped me. Now 1 final question.
                Everything works apart from the FTP. This is the error:
                Code:
                Do you want to upload your XML file to your website via FTP? Y/N: Y
                Server name: test
                Enter your login: test
                Enter your password: 
                Traceback (most recent call last):
                  File "rssgps.py", line 76, in <module>
                    finish()
                  File "rssgps.py", line 8, in finish
                    writea()
                  File "rssgps.py", line 25, in writea
                    ftpqe()
                  File "rssgps.py", line 43, in ftpqe
                    ftp()
                  File "rssgps.py", line 57, in ftp
                    ftplib.FTP(server,login,password)
                  File "/usr/lib/python2.5/ftplib.py", line 107, in __init__
                    self.connect(host)
                  File "/usr/lib/python2.5/ftplib.py", line 117, in connect
                    for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
                socket.gaierror: (-2, 'Name or service not known')
                Any ideas? Thanks

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  You did not assign the ftplib.FTP object to a variable. Try this:
                  [code=Python]
                  s = ftplib.FTP(serv er,login,passwo rd)
                  f = open('rss-feed.xml','rb')
                  s.storbinary('S TOR rss-feed.xml', f)
                  f.close()
                  s.quit()[/code]If that is not the problem, you may have an incorrect server name or user name. This worked for me on my FTP server:
                  [code=Python]>>> import ftplib
                  >>> f = ftplib.FTP('ftp .bvdetailing.co m', '*********', '********')
                  >>> f.mkd('ftplib_t est')
                  'ftplib_test'
                  >>> f.close()
                  >>> [/code]

                  Comment

                  • bhass
                    New Member
                    • Dec 2008
                    • 11

                    #10
                    Yet again, thank you. I can only keep thanking you!

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      You're welcome. You did a good job of defining your problem and seeing the thread through. Fortunately, I was able to help.

                      -BV

                      Comment

                      Working...