コード例 #1
0
 def test_is_binary(self):
     # basic tests
     self.assertFalse(is_binary("hello"))
     # utf-16 strings contain null bytes
     self.assertFalse(is_binary(u"hello".encode('utf-16')))
     # one with encoding
     self.assertFalse(is_binary("<div>Price \xa3</div>"))
     # finally some real binary bytes
     self.assertTrue(is_binary("\x02\xa3"))
コード例 #2
0
ファイル: factory.py プロジェクト: imclab/crawlmi
def from_body(body):
    '''Try to guess the appropiate response based on the body content.
    This method is a bit magic and could be improved in the future, but
    it's not meant to be used except for special cases where response types
    cannot be guess using more straightforward methods.
    '''
    chunk = body[:5000]
    if is_binary(chunk):
        return from_mime_type('application/octet-stream')
    elif '<html>' in chunk.lower():
        return from_mime_type('text/html')
    elif '<?xml' in chunk.lower():
        return from_mime_type('text/xml')
    else:
        return from_mime_type('text')