python - "AttributeError: ’dict’ object has no attribute ’filename’"
問(wèn)題描述
python2.7版本,使用Pycharm運(yùn)行python項(xiàng)目的gui.py文件時(shí)提示app.py的第17行代碼(也就是filename = i.file.filename)出錯(cuò),錯(cuò)誤信息:AttributeError: ’dict’ object has no attribute ’filename’但是代碼已經(jīng)對(duì)file進(jìn)行了初始化了:
i = web.input(file = {}) #接收數(shù)據(jù)filename = i.file.filename #獲取文件名file = i.file.file.read() #獲取文件
請(qǐng)問(wèn)為啥還是出現(xiàn)這個(gè)錯(cuò)誤?
html代碼為:
<!DOCTYPE html><html><body><form action='/upload' method='post' enctype='multipart/form-data'> <input type='file' name='file'> <input type='submit' value='post'></form></body></html>
gui.py代碼為:
# -*- coding:utf-8 -*-from Tkinter import *import tkFileDialogimport urllib2import sysimport win32clipboard as wimport win32conimport win32apiimport tkMessageBox# reload(sys)# sys.setdefaultencoding('utf-8')def setText(text): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_TEXT,text) w.CloseClipboard()def upload(): filename = tkFileDialog.askopenfilename(title='選擇文件')#選擇文件,返回文件名 files = open(filename,’rb’).read() data =’’’------WebKitFormBoundaryDLanRACtPqUEBKKsContent-Disposition: form-data; name='file'; filename='%s'Content-Type: application/octet-stream[file]------WebKitFormBoundaryDLanRACtPqUEBKKs--’’’%filename.split(’/’)[-1] data = bytes(data) data = data.replace(bytes(’[file]’),files) req = urllib2.Request(’http://127.0.0.1:8080/upload’,data) req.add_header(’Content-Type’,’multipart/form-data; boundary=----WebKitFormBoundaryPZsy5bHyBCEivf53’) html = urllib2.urlopen(req).read() print html ent.delete(0,END) ent.insert(0,html)def download(): files = urllib2.urlopen(ent.get()).read() filename = tkFileDialog.asksaveasfilename() with open(filename,’wb’) as fn:fn.write(files)def copy(): setText(ent.get()) tkMessageBox.showinfo(’ok’,’url已復(fù)制’)root = Tk()#創(chuàng)建窗口root.title('文件分享系統(tǒng)')#修改窗口名root.geometry('300x130+500+300')#修改窗口大小和位置ent = Entry(root,width = 50)#輸入框ent.grid()#顯示控件btn_upload = Button(root,text=' Upload ',command=upload)btn_upload.grid()btn_download = Button(root,text='Download',command=download)btn_download.grid()btn_copy = Button(root,text=' Copy url ',command=copy)btn_copy.grid()mainloop()#顯示窗口
app.py代碼為:
# -*- coding:utf-8 -*-import weburls = ( #’/my’,’My’,#瀏覽器訪問(wèn)http://127.0.0.1:8080/my時(shí),就會(huì)調(diào)用My這個(gè)類的GET方法 ’/’,’Index’, ’/upload’,’Upload’,)#路由render = web.template.render(’templates’)class Index: def GET(self):return render.index()class Upload: def POST(self):i = web.input(file = {}) #接收數(shù)據(jù)filename = i.file.filename #獲取文件名file = i.file.file.read() #獲取文件with open(’static/%s’ %filename,’wb’) as fn: fn.write(file)return ’http://127.0.0.1:8080/static/%s’ %filenameapp = web.application(urls,globals())if __name__== ’__main__’:#入口函數(shù)判斷,本文件調(diào)用時(shí),__name__== ’__main__’,其他文件調(diào)用時(shí),__name__==文件名 app.run()
問(wèn)題解答
回答1:上傳文件沒(méi)有成功。
問(wèn)題出在上傳的地方,這個(gè)Boudary后面的值不是固定的,urllib2沒(méi)有處理MIME的功能,要配合其它庫(kù)比如poster使用。
推薦你使用requests,Python里最好的http庫(kù)。
import requestsdata = {’file’: open(’a.out’,’rb’)} # 這里a.out是你要上傳的文件名requests.post(’http://127.0.0.1:8080/upload’,files=data)
相關(guān)文章:
1. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.2. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題3. 刷新頁(yè)面出現(xiàn)彈框4. javascript - 編程,算法的問(wèn)題5. javascript - 怎么看網(wǎng)站用了什么技術(shù)框架?6. mysql - 用PHPEXCEL將excel文件導(dǎo)入數(shù)據(jù)庫(kù)數(shù)據(jù)5000+條,本地?cái)?shù)據(jù)庫(kù)正常,線上只導(dǎo)入15條,沒(méi)有報(bào)錯(cuò),哪里的問(wèn)題?7. javascript - 請(qǐng)教空白文本節(jié)點(diǎn)的問(wèn)題8. PC 手機(jī)兼容的 編輯器9. 關(guān)于Android權(quán)限的獲取問(wèn)題,大家遇到過(guò)這樣的情況嘛?10. css - 關(guān)于border-image
