病毒总API

时间:2015-10-31 07:07:24

标签: python api

我想将API用于" Virustotal"为了扫描这些文件,我尝试了给出" https://www.virustotal.com/en/documentation/public-api/"但他们都没有为我工作。其中一个例子如下:

在ASP + Python文件的Virus Totals API中,我收到了错误消息。有两个文件,即Filescan.py和postfile.py。在重新运行Filescan.py的同时。

文件Filescan包含,

import sys
import postfile
host = "www.virustotal.com"
selector = "https://www.virustotal.com/vtapi/v2/file/scan"
fields = [("apikey", "api key")]
file_to_send = open("my file", "rb").read()
files = [("file","my file",file_to_send)]
json = postfile.post_multipart(host, selector, fields, files)
`enter code here`print (json)

postfile.py由

组成
import http.client, mimetypes
def post_multipart(host, selector, fields, files):
    content_type, body = encode_multipart_formdata(fields, files)
    h = http.client.HTTPS(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):
    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(str(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'

当我运行Filescan.py时,它会出现类似" AttributeError:module' http.client'没有属性' HTTPS'" 怎么解决这个? 有人知道Windows中Virustotal的工作API吗?

1 个答案:

答案 0 :(得分:0)

安装virus_total_apis模块,请检查:https://pypi.python.org/pypi/virustotal-api/

Url Scanner示例:

import virus_total_apis, json


def online_scan(api_key, target_url):

    switch_api_key = virus_total_apis.PublicApi(api_key)

    response = switch_api_key.get_url_report(target_url)
    raw_data_json = json.dumps(response, sort_keys=True, indent=4)
    raw_json_dict = json.loads(raw_data_json)

    return raw_json_dict

print(online_scan('your_api', 'www.google.com'))