小编Bri*_*ian的帖子

Python:如何从gzip压缩中流/管道数据?

我需要做这样的事情,但是在python中:

dd if=/dev/sdb | gzip -c | curl ftp upload
Run Code Online (Sandbox Code Playgroud)

我无法使用Popen的整个命令,因为:

  1. 我需要非阻塞操作
  2. 我需要进度信息(尝试循环通过proc.stderr无济于事)

另一件大事是我无法在上传之前在内存或磁盘上创建压缩的gzip文件.

所以这就是我想要弄清楚如何做,gzip_stream_of_strings(输入)是未知的:

import os, pycurl
filename = '/path/to/super/large/file.img'
filesize = os.path.getsize(filename)

def progress(dl_left, dl_completed, ul_left, ul_completed):
    return (ul_completed/filesize)*100

def main():
    c = pycurl.Curl()
    c.setopt(c.URL, 'ftp://IP/save_as.img.gz')
    c.setopt(pycurl.NOPROGRESS, 0)
    c.setopt(pycurl.PROGRESSFUNCTION, progress)
    c.setopt(pycurl.UPLOAD, 1)
    c.setopt(pycurl.INFILESIZE, filesize)
    c.setopt(pycurl.USERPWD, 'user:passwd')
    with open(filename) as input:
        c.setopt(pycurl.READFUNCTION, gzip_stream_of_stings(input))
        c.perform()
        c.close()
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏!

编辑:这是解决方案:

from gzip import GzipFile
from StringIO import StringIO

CHUNCK_SIZE = 1024

class GZipPipe(StringIO):
    """This class implements a compression pipe suitable for asynchronous …
Run Code Online (Sandbox Code Playgroud)

python gzip stream

9
推荐指数
1
解决办法
5605
查看次数

Flask-Admin&Authentication:"/ admin"受保护,但"/ admin/anything-else"不受保护

我正在尝试使用Flask和Flask-SuperAdmin自定义管理员视图,但是,索引视图和子视图显然没有使用相同的is_accessible方法:

编辑:我设法弄清楚我做错了什么.我需要在每个视图类中定义is_accessible.这是使用mixin-class完成的,如固定代码所示:

app/frontend/admin.py(固定和工作代码)

from flask.ext.security import current_user, login_required
from flask.ext.superadmin import expose, AdminIndexView
from flask.ext.superadmin.model.base import ModelAdmin
from ..core import db

# all admin views should subclass AuthMixin
class AuthMixin(object):
    def is_accessible(self):
        if current_user.is_authenticated() and current_user.has_role('Admin'):
            return True
        return False

# the view that gets used for the admin home page
class AdminIndex(AuthMixin, AdminIndexView):
    # use a custom template for the admin home page
    @expose('/')
    def index(self):
        return self.render('admin/index.jade')

# base view for all other …
Run Code Online (Sandbox Code Playgroud)

python flask flask-login flask-security flask-admin

9
推荐指数
1
解决办法
4083
查看次数

标签 统计

python ×2

flask ×1

flask-admin ×1

flask-login ×1

flask-security ×1

gzip ×1

stream ×1