1. 程式人生 > >Web靜態伺服器-1-顯示固定的頁面

Web靜態伺服器-1-顯示固定的頁面

#coding=utf-8
import socket
from multiprocessing import Process

def handleClient(clientSocket):
    '用一個新的程序,為一個客戶端進行服務'
    recvData = clientSocket.recv(2014)
    requestHeaderLines = recvData.splitlines()
    for line in requestHeaderLines:
        print(line)

    responseHeaderLines = "HTTP/1.1 200 OK\r\n"
    responseHeaderLines += "\r\n"
    responseBody = "hello world"

    response = responseHeaderLines + responseBody
    clientSocket.send(response)
    clientSocket.close()

def main():
    '作為程式的主控制入口'
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serverSocket.bind(("", 7788))
    serverSocket.listen(10)
    while True:
        clientSocket,clientAddr = serverSocket.accept()
        clientP = Process(target = handleClient, args = (clientSocket,))
        clientP.start()
        clientSocket.close()

if __name__ == '__main__':
    main()

伺服器端

客戶端