图片上传和展示

普遍接受的预防XSRF攻击的方案是让每个用户的cookie都是不确定的值, 并且 把那个cookie值在你站点的每个form提交中作为额外的参数包含进来. 如果cookie 和form提交中的值不匹配, 则请求可能是伪造的

路由

xsrf_cookies= True,

所有通过 POST 请求的form提交添加这个字段. 你可以使用一个特性的 UIModulexsrf_form_html() 来做这件事情, 这个方法在所有模板中都是可用的:

列表上传到字典,再到表单
class UploadHandler(BaseHandelr):
def get(self):
self.render('07upload.html')

def post(self):
pics = self.request.files.get('picture', [])
print(pics[0]['filename'])
#pics = [{"filename":..., "content_type":..., "body":...},]
for p in pics:
print(p['filename'])
print(p['content_type'])
print(p['body'])

self.write('upload done')

class UploadHandler(BaseHandelr):
def get(self):
self.render('07upload.html')

def post(self):
pics = self.request.files.get('picture', [])
print(pics[0]['filename'])
#pics = [{"filename":..., "content_type":..., "body":...},]
for p in pics:
save_path = 'static/upload/{}'.format(p['filename'])
print(save_path)
#print(p['filename'])
print(p['content_type'])
with open(save_path, 'wb') as fh:
fh.write(p['body'])

self.write('upload done')
 

 上传文件到服务器

虚拟机更改工作目录

os.getcwd()

os.chdir('static')

os.getcwd()

ns = glob.glob(r'uploda/*')

ns

猜你喜欢

转载自www.cnblogs.com/wdty/p/10824541.html