使用VirusTotal API扫描文件

时间:2012-08-08 18:36:49

标签: python python-3.x

我在Python中相当新,所以这对某些人来说可能是个问题。我是在Python 3.0中的DEVING

我一直遇到错误:

File "scan.py", line 7, in module
    json = postfile.post_multipart(host,selector,fields,files)
File "C:\Python32\lib\postfile.py", line 10, in post_multipart
    content_type, body = encode_multipart_formdata(fields,files)
File "C:\Python32\lib\postfile.py", line 42, encode_multipart_fordata
     body = CRLF.join(L)
TypeError: sequence item 8: expected str instance, bytes found

当我尝试运行此代码以使用VirusTotal API连接和扫描文件时。此代码与站点中的示例类似。

import postfile
host = "www.virustotal.com"
selector = "https://www.virustotal.com/vtapi/v2/file/scan"
fields = [("apikey", "123123123123123123123123123")]
file_to_send = open("android-icq.apk", "rb").read()
files = [("file", "android-icq.apk", file_to_send)]
json = postfile.post_multipart(host, selector, fields, files)
print (json)

postfile.py内容如下:

import http.client, mimetypes

def post_multipart(host, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return the server's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
    h = http.client.HTTP(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers = h.getreply()
    return h.file.read()

def encode_multipart_formdata(fields, files):
    """
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'

这里有关于这个问题的任何想法吗?

2 个答案:

答案 0 :(得分:2)

看起来这些示例是用Python 2编写的。您使用的是Python 3。

Python 3背后的部分理由是删除了'cruft',它是Python开发多年来积累的,所以它允许在某些地方打破向后兼容性。

见这里:http://wiki.python.org/moin/Python2orPython3

答案 1 :(得分:0)

http://docs.python-requests.org/为HTTP 1.1查询提供了更清晰的界面:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});