python - "AttributeError: ’dict’ object has no attribute ’filename’"
問題描述
python2.7版本,使用Pycharm運行python項目的gui.py文件時提示app.py的第17行代碼(也就是filename = i.file.filename)出錯,錯誤信息:AttributeError: ’dict’ object has no attribute ’filename’但是代碼已經(jīng)對file進行了初始化了:
i = web.input(file = {}) #接收數(shù)據(jù)filename = i.file.filename #獲取文件名file = i.file.file.read() #獲取文件
請問為啥還是出現(xiàn)這個錯誤?
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’,#瀏覽器訪問http://127.0.0.1:8080/my時,就會調(diào)用My這個類的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)用時,__name__== ’__main__’,其他文件調(diào)用時,__name__==文件名 app.run()
問題解答
回答1:上傳文件沒有成功。
問題出在上傳的地方,這個Boudary后面的值不是固定的,urllib2沒有處理MIME的功能,要配合其它庫比如poster使用。
推薦你使用requests,Python里最好的http庫。
import requestsdata = {’file’: open(’a.out’,’rb’)} # 這里a.out是你要上傳的文件名requests.post(’http://127.0.0.1:8080/upload’,files=data)
相關(guān)文章:
1. 數(shù)組按鍵值封裝!2. java - web項目中,用戶登陸信息存儲在session中好 還是cookie中好,取決于什么?3. angular.js - webpack build后的angularjs路由跳轉(zhuǎn)問題4. Mysql取下一條記錄5. mysql - 查詢字段做了索引為什么不起效,還有查詢一個月的時候數(shù)據(jù)都是全部出來的,如果分拆3次的話就沒問題,為什么呢。6. mysql - 大部分數(shù)據(jù)沒有行溢出的text字段是否需要拆表7. 老師,怎么不講一次性添加多個數(shù)據(jù)8. python - linux 下用wsgifunc 運行web.py該如何修改代碼9. pdo - mysql 簡單注入疑問10. 表格對其 只涉及到對其,沒有涉及到大小,長寬還有背景色類的嗎
