【问题标题】:Scanning File with VirusTotal API使用 VirusTotal API 扫描文件
【发布时间】:2017-04-05 15:12:14
【问题描述】:

我是 Python 的新手,所以这对某些人来说可能是个菜鸟问题。我正在使用 Python 3.0 进行开发

我不断遇到错误:

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'

对这里的问题有什么想法吗?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

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

    Python 3 背后的部分原因是移除了 Python 多年开发过程中积累的“杂乱无章”,因此允许它在某些地方破坏向后兼容性。

    请看这里:http://wiki.python.org/moin/Python2orPython3

    【讨论】:

    • 谢谢你。但是,我能够使他们的其他样本工作。这只是我无法运行的特定示例。我使用 2to3 工具将 2.x 代码转换为 3。如果我仍然无法让它工作,那么我将考虑使用 2.x python。非常感谢!
    • 我已经切换到 2.7 python 并且代码运行良好。这不是一个解决方案,但至少它有效! :) 谢谢!
    【解决方案2】:

    http://docs.python-requests.org/ 提供更简洁的接口来进行 HTTP 1.1 查询:

    import time
    import requests
    import json
    
    md5url = "https://www.virustotal.com/vtapi/v2/file/report"
    r = requests.post(requrl, data = {"apikey": apikey, "resource": md5})
    print(json.loads(r.text))
    print(json.loads(r.text)["positives"])
    time.sleep(15)
    
    scanurl = "https://www.virustotal.com/vtapi/v2/file/scan"
    r = requests.post(scanurl, data = {"apikey": apikey}, files = {"file": (name, open(name, "rb"))})
    print(json.loads(r.text))
    print(json.loads(r.text)["permalink"])
    time.sleep(15)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      相关资源
      最近更新 更多