简单调试前端 html js css woff ok

#! /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
import time
from datetime import datetime

global listen_socket


def fun(str):
    global listen_socket

    while True:
        client_connection, client_address = listen_socket.accept()
        request = client_connection.recv(2048)
        print "re--", request
        if request == "":
            continue
        method = request.split(' ')[0]
        src = request.split(' ')[1]
        print src
        #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>
        '''
        if method == 'GET':
            # ULR
            print "-------------",src
            file_name = src[1:len(src)]
            #if src == '/test.jpg':
            if -1 != src.find("/static/css/"):
                content = read_file(file_name, "text/css")
            elif -1 != src.find("/static/js/"):
                    content = read_file(file_name, "application/x-javascript")
            elif -1 != src.find("/static/fonts/"):
                    content = read_file(file_name, "application/font-woff")
            elif src== "/favicon.ico":
                content = read_file(file_name, "application/x-ico")
            elif -1 != src.find(".jpg"):
                content = read_file(file_name, "image/jpeg")
            elif -1 != src.find(".gif"):
                content = read_file(file_name, "image/gif")
            else:
                content = http_response

            print 'Connected by', client_address
            print 'Request is:', request
            #print 'content is:', content
            client_connection.sendall(content)
        # close connection
        client_connection.close()

def init_web():
    global listen_socket
    HOST, PORT = 'localhost', 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 datetime_filter(t):
    delta = int(time.time() - t)
    if delta < 60:
        return u'1分钟前'
    if delta < 3600:
        return u'%s分钟前' % (delta // 60)
    if delta < 86400:
        return u'%s小时前' % (delta // 3600)
    if delta < 604800:
        return u'%s天前' % (delta // 86400)
    dt = datetime.fromtimestamp(t)
    return u'%s年%s月%s日' % (dt.year, dt.month, dt.day)
class User():
    name = u"哦用戶"
class Blog():
    created_at = 3.3
    name =u"地方"
    id =3
class Comment():
    created_at = 3000003
    user_name =u"地dfdfds地方方"
    user_id =3
class Page():
    created_at = 3000003
    user_name =u"地dfdfds地方方"
    page_index =3

def read_file(file_name, file_type):
    print "---------------------------",file_name
    f = open(file_name, 'rb')
    pic_content = '''\nHTTP/1.x 200 OK\nContent-Type: %s\n\n''' % (file_type)
    pic_content = pic_content + f.read()
    f.close()
    return pic_content


def init_jinja():
    env = Environment(loader=PackageLoader('main', 'templates'))
    env.filters['datetime'] = datetime_filter
    template = env.get_template('about.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士大夫"
    blog = Blog()

    blog.created_at=90000
    user = User()
    comment =Comment()
    page=Page()
    output = template.render(blog=blog,user=user,comment=comment, page=page).encode("utf-8")
    # output2 = env.get_template('base.html').render().encode('utf-8')
    return output


if __name__=='__main__':
    init_web()
    output = init_jinja()
    #print output
    fun(output)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值