2012-08-08 137 views
2

我在Python中相當新,所以這可能是一些問題。我DEVing在Python 3.0用VirusTotal API掃描文件

我不斷遇到錯誤:

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

它看起來像這些例子用Python編寫2.您正在使用Python 3

背後的Python 3一部分理由就是消除「克魯夫特」,這已建立了多年來Python的發展,所以它被允許在某些地方打破向後兼容性。

在這裏看到:http://wiki.python.org/moin/Python2orPython3

+0

感謝你爲這個。但是,我能夠使他們的其他樣品工作。只是這個具體的例子,我無法運行。 我用2to3工具將2.x代碼轉換爲3. 如果我仍然不會得到這個工作,那麼我會考慮去2.x python。 非常感謝! – gwafito 2012-08-09 11:18:40

+0

我切換到2.7 python和代碼工作正常。這不是一個解決方案,但至少它工作! :) 謝謝! – gwafito 2012-08-09 12:11:58

0

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)