为什么我的python代码没有';不行?

为什么我的python代码没有';不行?,python,http,python-imaging-library,Python,Http,Python Imaging Library,执行此操作时,出现一个错误: def getSize(f): print StringIO(f) im = Image.open(StringIO(f)) size = im.size[0], im.size[1] return size def download(source_url, g = False, correct_url = True): try: socket.setdefaulttimeout(10) ag

执行此操作时,出现一个错误:

def getSize(f):
    print StringIO(f)
    im = Image.open(StringIO(f))
    size = im.size[0], im.size[1]
    return size

def download(source_url, g = False, correct_url = True):
    try:
        socket.setdefaulttimeout(10)
        agents = ['Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)','Microsoft Internet Explorer/4.0b1 (Windows 95)','Opera/8.00 (Windows NT 5.1; U; en)']
        ree = urllib2.Request(source_url)
        ree.add_header('User-Agent',random.choice(agents))
        ree.add_header('Accept-encoding', 'gzip')
        opener = urllib2.build_opener()
        h = opener.open(ree).read()
        if g:
            compressedstream = StringIO(h)
            gzipper = gzip.GzipFile(fileobj=compressedstream)
            data = gzipper.read()
            return data
        else:
            return h
    except Exception, e:
        return ""



pic = download("http://media2.ct.yelpcdn.com/photo/2MdauidaMUazuew2h0pdgQ/l")
s = getSize(pic)

问题是您的
Accept Encoding
状态为
gzip
,因此您可能会得到一个gzip图像

我刚刚在gzip解压的情况下尝试了你的代码,它运行起来没有任何问题

print StringIO(f)
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
'Accept-Encoding'
'gzip'
更改为
'image.*'

    pic = download("http://media2.ct.yelpcdn.com/photo/2MdauidaMUazuew2h0pdgQ/l", g=True)
    s = getSize(pic)

第2部分:

如果gzip投诉,您可以随时请求
gzip
并在此处使用try/except进行包装,以返回未更改的数据

    ree.add_header('User-Agent',random.choice(agents))
    ree.add_header('Accept-Encoding', 'image.*')

问题是您的
Accept Encoding
状态为
gzip
,因此您可能会得到一个gzip图像

我刚刚在gzip解压的情况下尝试了你的代码,它运行起来没有任何问题

print StringIO(f)
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
'Accept-Encoding'
'gzip'
更改为
'image.*'

    pic = download("http://media2.ct.yelpcdn.com/photo/2MdauidaMUazuew2h0pdgQ/l", g=True)
    s = getSize(pic)

第2部分:

如果gzip投诉,您可以随时请求
gzip
并在此处使用try/except进行包装,以返回未更改的数据

    ree.add_header('User-Agent',random.choice(agents))
    ree.add_header('Accept-Encoding', 'image.*')

您正在丢弃download()将抛出的任何有用异常-Image.open()可能正在尝试读取download()返回的“”,这将解释您当前收到的错误。如果删除try..catch块,是否会收到更多信息错误?重新标记以删除django标记-此处不依赖django您只是丢弃了download()将抛出的任何有用异常-Image.open()可能正在尝试读取download()返回的“”,这将解释您收到的当前错误。如果删除try..catch块,是否会收到更多信息错误?重新标记以删除django标记-此处不依赖django如何更改负责所有下载的下载函数,不需要注册gzip或图像?我想您可以将gzip函数包装在try/except块中,但是服务器不应该向您发送不在接受编码中的内容类型。因为这听起来极不可能,如果你想这样做,我会在你尝试用PIL打开它之后测试响应是否是gzip。我如何更改一个负责所有下载的下载函数,不需要gzip或image?我想你可以将gzip函数包装在一个try/except块中,但是服务器不应该向您发送不在接受编码中的内容类型。因为这听起来非常不可能,如果您想这样做,我会在您尝试用PIL打开它之后测试响应是否是gzip。