Pyhon + Socket(TCP)编程实现简单的WebServer

 

11

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   Hello, my WSGI server!
 <form method="post" action="main.html">
     <input type="text" name="userName">
     <input type="password" name="userPwd">
     <input type="submit"  />
 </form>
</body>
</html

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<img src="static/photo1.png" />
System main page!
</body>
</html>

application.py

import socket
import threading
from response import HttpResponse
from request  import HttpRequest

# WSGI服务器
class WSGIServer():

    def __init__(self, host='localhost', port=8080, connectSize=100):
        '''
        :param port: 服务器的端口号
        :param connectSize: 默认的并发数量
        '''
        self.__host = host
        self.__port = port
        self.__connectSize = connectSize
        pass

    def startServer(self):
        '''
        服务启动主程序
        :return:
        '''
        server = None
        try:
            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            server.bind((self.__host, self.__port))
            server.listen(self.__connectSize)
            while True:
                print("======服务器启动成功:http://" + self.__host + ":" + str(self.__port))
                clientConn, clientAddr = server.accept()  # 等待客户端请求
                # 启动独立的线程,处理每一次用户请求
                wt = WorkThread(clientConn, clientAddr)
                wt.start()
                pass
        except socket.gaierror as g:
            print(g)
        finally:
            if server:
                server.close()
        pass

    pass

class WorkThread(threading.Thread):
    def __init__(self, connection, addr, bufferSize=8096):
        threading.Thread.__init__(self)
        self.__connection = connection
        self.__addr = addr
        self.__bufferSize = bufferSize
        pass

    def run(self):
        receiveMsg = self.__connection.recv(self.__bufferSize)
        receiveMsg = receiveMsg.decode("utf-8")
        print(receiveMsg)
        request = HttpRequest()
        params = request.parseRequest(receiveMsg)
        responseText = ""
        response = HttpResponse()
        if params['Accept'].find('text/html') >=0:
            url = 'templates/index.html'
            if params['url'] == "/":
                url = 'templates/index.html'
            else:
                url = 'templates' + params['url']
                if params['url'] == '/main.html':
                    if params.get('dataparams').get('userName') != 'zhangsan':
                        url = 'templates/index.html'
                        responseText = response.responseHeader('text/html', 200) + "\n" + response.responseBodyText(url)
                        pass
                    pass
            responseText = response.responseHeader('text/html', 200) + "\n" + response.responseBodyText(url)
            self.__connection.send(responseText.encode("utf-8"))
            pass
        elif params['Accept'].find('image/') >= 0:
            url = params['url']
            self.__connection.send(response.responseHeader('image/', 200).encode("utf-8"))
            self.__connection.send("\n".encode("utf-8"))
            self.__connection.send(response.responseBodyBinary(url[1:]))
            pass
            print(responseText)

        self.__connection.close()

        pass
    pass

request.py

class HttpRequest():

    def parseRequest(self, requestText):
        params = {}
        lineArray = requestText.split('\r\n')
        row = 1
        isBody = False
        bodyText = ""
        for line in lineArray:
            if row == 1 :
                array = line.split(' ')
                params['method'] = array[0]
                params['url'] = array[1]
                params['httptype'] = array[2]
                row += 1
            elif line.strip() == "":
                isBody = True

            elif not isBody:
                array = line.split(':')
                params[array[0]] = line[len(array[0])+2:]
                pass
            elif isBody:
                bodyText += line
            pass

        params['body'] = bodyText
        dataParams = {}
        bodyText = bodyText.strip()
        if params.get('Content-Type') == 'application/x-www-form-urlencoded' and   params['method'] == 'POST':
            array = bodyText.split('&')
            for data in array:
                keyvalue = data.split("=")
                dataParams[keyvalue[0]] = keyvalue[1]
                pass
            pass
        params['dataparams'] = dataParams
        return params
        pass
    pass

response.py

class HttpResponse():
    def responseHeader(self, contentType, ressponseCode):
        header = "http/1.1 200 OK\r\n"
        if contentType == 'text/html':
                header += "Content-Type: text/html\r\n" + \
                 "X-Ua-Compatible: IE=Edge,chrome=1\r\n"
        elif contentType == "image/":
            header += "Content-Type: image/png\r\n" + \
                      "X-Ua-Compatible: IE=Edge,chrome=1\r\n"
        return header
        pass

    def responseBodyText(self, url):
        body = ""
        with open(url, 'r') as fp:
            body = fp.read()
        return body
        pass

    def responseBodyBinary(self, url):
        body = b""
        with open(url, 'rb') as fp:
            body = fp.read()
        return body
    pass

runserver.py

from application import WSGIServer

if __name__ == '__main__':
    # 创建服务器对象
    wsgiServer = WSGIServer()
    wsgiServer.startServer()
    pass

 

 

 

                                                             end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值