Daily 1229

Django zip files (create dynamic in-memory archives with Python’s zipfile) 动态打包文件 并输出到浏览器端


from StringIO import StringIO
from zipfile import ZipFile
from django.http import HttpResponse

def download(request, company_id):

    in_memory = StringIO()
    zip = ZipFile(in_memory, "a")

    zip.writestr("file1.txt", "some text contents")
    zip.writestr("file2.csv", "csv,data,here")

    # fix for Linux zip files read in Windows
    for file in zip.filelist:
        file.create_system = 0

    zip.close()

    response = HttpResponse(mimetype="application/zip")
    response["Content-Disposition"] = "attachment; filename=two_files.zip"

    in_memory.seek(0)
    response.write(in_memory.read())

    return response

Using the Forwarded header

proxy_set_header Forwarded $proxy_add_forwarded;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

获取真实的地址: How do I get the client IP of a Tornado request?

x_real_ip = self.request.headers.get("X-Real-IP")
remote_ip = x_real_ip or self.request.remote_ip
最后修改 February 7, 2021 : doc: 整理daily日记 (0dcc57d)