python如何編寫一個簡單的WSGIPDFserver

這篇文章給大家分享的是有關python如何編寫一個簡單的WSGI PDF server的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)公司是一家專注于成都做網(wǎng)站、成都網(wǎng)站建設與策劃設計,鎮(zhèn)寧網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設10余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:鎮(zhèn)寧等地區(qū)。鎮(zhèn)寧做網(wǎng)站價格咨詢:13518219792

示例:

# basic_wsgi_pdf_server.py
# Basic WSGI PDF server in Python.
# Adapted from:

from PDFWriter import PDFWriter
from wsgiref.simple_server import make_server

host = 'localhost'
port = 8888

def app(environ, start_response):
    path = environ['PATH_INFO']
    method = environ['REQUEST_METHOD']
    print "path:", path
    print "method:", method

    #response = 'This is the page for "{}"'.format(path)

    lines = [
            "Jack and Jill went up the hill",
            "Humpty Dumpty sat on a wall,",
            "'You are old, Father William,' the young man said,",
            "Master of all masters"
            ]

    pdf_filename = "Nursery-rhymes-and-stories.pdf"
    pw = PDFWriter(pdf_filename)
    pw.setFont("Courier", 12)
    pw.setHeader("Excerpts from nursery rhymes and stories")
    pw.setFooter("Generated by xtopdf and basic_wsgi_pdf_server")

    for line in lines:
        pw.writeLine(line)
        pw.writeLine(" ")
    pw.close()

    with open(pdf_filename, "rb") as fil:
        response = fil.read()

    #start_response('200 OK', [('Content-type', 'text/html')])
    start_response('200 OK', [('Content-type', 'application/pdf')])
    return [response]

make_server(host, port, app).serve_forever()

在命令行下運行下面的代碼開啟服務:

pythonbasic_wsgi_pdf_server.py

感謝各位的閱讀!關于“python如何編寫一個簡單的WSGI PDF server”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章名稱:python如何編寫一個簡單的WSGIPDFserver
分享地址:http://muchs.cn/article26/jejjjg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站營銷、外貿(mào)建站、網(wǎng)站改版、網(wǎng)站設計公司、外貿(mào)網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站