[Python] python编写的一个简单那的WSGI PDF server →→→→→进入此内容的聊天室

来自 , 2020-06-06, 写在 Python, 查看 123 次.
URL http://www.code666.cn/view/e6e9099e
  1. # basic_wsgi_pdf_server.py
  2. # Basic WSGI PDF server in Python.
  3. # Adapted from:
  4.  
  5. # http://www.reddit.com/r/Python/comments/1eboql/python_website_tuts_that_dont_use_django/c9z3qyz
  6.  
  7. from PDFWriter import PDFWriter
  8. from wsgiref.simple_server import make_server
  9.  
  10. host = 'localhost'
  11. port = 8888
  12.  
  13. def app(environ, start_response):
  14.     path = environ['PATH_INFO']
  15.     method = environ['REQUEST_METHOD']
  16.     print "path:", path
  17.     print "method:", method
  18.  
  19.     #response = 'This is the page for "{}"'.format(path)
  20.  
  21.     lines = [
  22.             "Jack and Jill went up the hill",
  23.             "Humpty Dumpty sat on a wall,",
  24.             "'You are old, Father William,' the young man said,",
  25.             "Master of all masters"
  26.             ]
  27.  
  28.     pdf_filename = "Nursery-rhymes-and-stories.pdf"
  29.     pw = PDFWriter(pdf_filename)
  30.     pw.setFont("Courier", 12)
  31.     pw.setHeader("Excerpts from nursery rhymes and stories")
  32.     pw.setFooter("Generated by xtopdf and basic_wsgi_pdf_server")
  33.  
  34.     for line in lines:
  35.         pw.writeLine(line)
  36.         pw.writeLine(" ")
  37.     pw.close()
  38.  
  39.     with open(pdf_filename, "rb") as fil:
  40.         response = fil.read()
  41.  
  42.     #start_response('200 OK', [('Content-type', 'text/html')])
  43.     start_response('200 OK', [('Content-type', 'application/pdf')])
  44.     return [response]
  45.  
  46. make_server(host, port, app).serve_forever()
  47.  
  48.  
  49. #//python/8982

回复 "python编写的一个简单那的WSGI PDF server "

这儿你可以回复上面这条便签

captcha