python jinja 简单调试

#! /usr/bin/python2
# coding=utf-8
'''
from jinja2 import Template

if __name__=='__main__':
    template = Template('Hello {{ name }}!')
    print template.render(name='John Doe')
'''
from jinja2 import Environment, PackageLoader
import socket

global listen_socket

def init_web():
    global listen_socket
    HOST, PORT = '', 8888

    listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    listen_socket.bind((HOST, PORT))
    listen_socket.listen(1)
    print 'Serving HTTP on port %s ...' % PORT

def fun(str):
    global listen_socket

    while True:
        client_connection, client_address = listen_socket.accept()
        request = client_connection.recv(2048)
        print "re--", request
        print "-----------------------------------------------------", str
        http_response = '''HTTP/1.x 200 OK
Content-Type: text/html; charset=utf-8


'''+str
        '''
        <head>
<title>WOWzh</title>
</head>
<html>
<p>Wow, Pythond 说的Sserver</p>

</html>
        '''

        client_connection.sendall(http_response)
        client_connection.close()


if __name__=='__main__':
    env = Environment(loader=PackageLoader('main', 'templates'))
    template = env.get_template('c1.html')
    http_response = '''HTTP/1.x 200 OK
    Content-Type: text/html; charset=utf-8

    <head>
    <title>WOWzh</title>
    </head>
    <html>
    <p>Wow, Pythond 说的Sserver</p>

    </html>
    '''
    st=u"1士大夫"

    output = template.render(Index=st).encode("utf-8")
    print output
    init_web()
    #output2 = env.get_template('base.html').render().encode('utf-8')
    fun(output)

--------------------------------c1.html-----------------------------
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
    {{ super() }}
    <style type="text/css">
        .important { color: #336699; }
    </style>
{% endblock %}
{% block content %}
    <h1>{{Index}} 的说的</h1>
    <p class="important">
      Welcome to my awesome homepage.
    </p>
{% endblock %}

----------------------base.html-----------------
<!DOCTYPE html>
<html lang="utf-8">
<head>
    {% block head %}
    <link rel="stylesheet" href="style.css" />
    <title>{% block title %}{% endblock %} - My Webpage</title>
    {% endblock %}
</head>
<body>
    <div id="content">{% block content %}{% endblock %}</div>
    <div id="footer">
        {% block footer %}
        &copy; Copyright 2008 by <a href="http://domain.invalid/">you</a>.
        {% endblock %}
    </div>
</body>
</html>

--------------------------------浏览器打开-----------
http://127.0.0.1:8888/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值