BadZipfile "file is not a zip file"

webcomm ryandw at gmail.com
Mon Jan 12 17:28:50 CET 2009


If anyone's interested, here are my django views...


from django.shortcuts import render_to_response
from django.http import HttpResponse
from xml.etree.ElementTree import ElementTree
import urllib, base64, subprocess

def get_data(request):
    service_url = 'http://www.something.com/webservices/someservice/
etc?user=etc&pass=etc'
    xml = urllib.urlopen(service_url)
    #the base64-encoded string is in a one-element xml doc...
    tree = ElementTree()
    xml_doc = tree.parse(xml)
    datum = ""
    for node in xml_doc.getiterator():
         datum = "%s" % (node.text)
    decoded = base64.b64decode(datum)

    dir = '/path/to/data/'
    f = open(dir+'data.zip', 'wb')
    f.write(decoded)
    f.close()

    file = subprocess.call('unzip '+dir+'data.zip -d '+dir,
shell=True)
    file = open(dir+'data', 'rb').read()
    txt = file.decode('utf_16_le')

    return render_to_response('output.html',{
        'output' : txt
        })

def read_xml(request):
    xml = urllib.urlopen('http://www.something.org/get_data/')  #page
using the get_data view
    xml = xml.read()
    xml = unicode(xml)
    xml = '<?xml version="1.0" encoding="UTF-8"?>\n<stuff>'+xml+'</
stuff>'

    f = open('/path/to/temp.txt','w')
    f.write(xml)
    f.close()

    tree = ElementTree()
    xml_doc = tree.parse('/path/to/temp.txt')
    datum = ""
    for node in xml_doc.getiterator():
         datum = "%s<br />%s - %s" % (datum, node.tag, node.text)

    return render_to_response('output.html',{
        'output' : datum
        })





More information about the Python-list mailing list