zoukankan      html  css  js  c++  java
  • django框架,小型简单服务器搭建,获取姓名,html读取和url读取

    这个接口就是WSGI:Web Server Gateway Interface。web网关接口。

    from wsgiref.simple_server import make_server #wsgiref是服务器类似于njax
    #调用服务器模块下的make_server
    #这只是模拟服务器,以后肯定是njax或者是apache
    
    #定义函数,参数
    def application(environ, start_response):
        #print(environ)#封装请求信息,键值对形式显示请求者的信息
        start_response('200 OK', [('Content-Type', 'text/html')])#响应头文本类型,是。。(),(),表述
        return [b'<h1>Hello, web!</h1>']#b是字节类型这里是想应体
    
    
    httpd = make_server('', 8080, application)#使用makeserver,第一步先运行这里,然后application函数
    print('Serving HTTP on port 8000...')
    # 开始监听HTTP请求:
    httpd.serve_forever()

    先运行,然后再浏览器输入127.0.0.1:8000可访问,浏览器模拟客户端,py模拟服务器

    from wsgiref.simple_server import make_server #wsgiref是服务器类似于njax
    #调用服务器模块下的make_server
    #这只是模拟服务器,以后肯定是njax或者是apache
    
    #定义函数,参数
    def application(environ, start_response):
        print("path" ,environ["PATH_INFO"])
        path=environ["PATH_INFO"]
        if path=="/alex":
            return [b'<h1>Hello, alex!</h1>']
        elif path=="/tom":
            return [b'<h1>Hello, tom!</h1>']
        else:
            return [b"404"]
        start_response('200 OK', [('Content-Type', 'text/html')])#响应头文本类型,是。。(),(),表述
    
    
    
    httpd = make_server('', 8080, application)#使用makeserver,第一步先运行这里,然后application函数
    print('Serving HTTP on port 8000...')
    # 开始监听HTTP请求:
    httpd.serve_forever()

    输入127.0.0.1:8000/tom,网页显示欢应tom

    from wsgiref.simple_server import make_server #wsgiref是服务器类似于njax
    #调用服务器模块下的make_server
    #这只是模拟服务器,以后肯定是njax或者是apache
    def fool():
        f=open("abc.html","rb")
        data=f.read()
        return data
    def fool2():
        f = open("abc.html", "rb")
        data = f.read()
        return data
    #定义函数,参数
    def application(environ, start_response):
        print("path" ,environ["PATH_INFO"])
        start_response('200 OK', [('Content-Type', 'text/html')])
        
        
        path=environ["PATH_INFO"]
        if path=="/alex":
            #return [b'<h1>Hello, alex!</h1>']
            return fool2()
        elif path=="/tom":
            #return [b'<h1>Hello, tom!</h1>']
            return fool()
        else:
            return [b"404"]
        
    
    
    
    httpd = make_server('', 8080, application)
    print('Serving HTTP on port 8000...')
    # 开始监听HTTP请求:
    httpd.serve_forever()
    整个application()函数本身没有涉及到任何解析HTTP的部分,也就是说,底层代码不需要我们自己编写,
    我们只负责在更高层次上考虑如何响应请求就可以了。
    
    application()函数必须由WSGI服务器来调用。有很多符合WSGI规范的服务器,我们可以挑选一个来用。
    
    Python内置了一个WSGI服务器,这个模块叫wsgiref    
        
        
    application()函数就是符合WSGI标准的一个HTTP处理函数,它接收两个参数:
    
            //environ:一个包含所有HTTP请求信息的dict对象;
            
            //start_response:一个发送HTTP响应的函数。
    
    在application()函数中,调用:
    
    start_response('200 OK', [('Content-Type', 'text/html')])
    
    就发送了HTTP响应的Header,注意Header只能发送一次,也就是只能调用一次start_response()函数。
    start_response()函数接收两个参数,一个是HTTP响应码,一个是一组list表示的HTTP Header,每
    个Header用一个包含两个str的tuple表示。
    
    通常情况下,都应该把Content-Type头发送给浏览器。其他很多常用的HTTP Header也应该发送。
    
    然后,函数的返回值b'<h1>Hello, web!</h1>'将作为HTTP响应的Body发送给浏览器。
    
    有了WSGI,我们关心的就是如何从environ这个dict对象拿到HTTP请求信息,然后构造HTML,
    通过start_response()发送Header,最后返回Body。
    print(environ['PATH_INFO'])
        path=environ['PATH_INFO']
        start_response('200 OK', [('Content-Type', 'text/html')])
        f1=open("index1.html","rb")
        data1=f1.read()
        f2=open("index2.html","rb")
        data2=f2.read()
    
        if path=="/yuan":
            return [data1]
        elif path=="/alex":
            return [data2]
        else:
            return ["<h1>404</h1>".encode('utf8')]
  • 相关阅读:
    JavaScript中的闭包
    正则表达式(括号)、[中括号]、{大括号}的区别
    写出将字符串中的数字转换为整型的方法,如:“as31d2v”->312,并写出相应的单元测试,正则去掉非数值、小数点及正负号外的字符串
    正则替换实现字符串链接每4位用“-”连接成新的字符串
    memcache搭建
    MySQL优化
    网络优化
    JDK配置及tomcat部署
    oracle中增加pga和sga
    sudo用法
  • 原文地址:https://www.cnblogs.com/wfl9310/p/9362886.html
Copyright © 2011-2022 走看看