头歌实训答案:WEB服务器编程实现

WEB服务器编程实现

第1关:创建流式套接字

#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM) 
#Prepare a sever socket 
########## Begin ##########
serverSocket.bind(('127.0.0.1',6789))
serverSocket.listen(1)
########## End ##########
print(serverSocket)
serverSocket.close()

第2关:服务端获取连接请求

#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM) 
#Prepare a sever socket 
serverSocket.bind(("127.0.0.1",6789))
serverSocket.listen(1)

#while True:
#Establish the connection
print('开始WEB服务...')

try:
    ########## Begin ##########
    connectionSocket, addr = serverSocket.accept()
    message = connectionSocket.recv(1024)


    ########## End ##########
    print(addr[0])
    print(message)
    connectionSocket.close()
except IOError:

        connectionSocket.close()
serverSocket.close()

第3关:服务端读取请求文件内容

#import socket module
from socket import *

serverSocket = socket(AF_INET, SOCK_STREAM) 
#Prepare a sever socket 
serverSocket.bind(("127.0.0.1",6789))
serverSocket.listen(1)

#while True:
print('开始WEB服务...')

try:
    connectionSocket, addr = serverSocket.accept()
    message = connectionSocket.recv(1024) # 获取客户发送的报文
        
    #读取文件内容
    ######### Begin #########
    name = message.split()
    name = name[1].decode('utf-8')
    name = name.replace("/","")
    outputdata = open(name,'r').read()



    ######### End #########
    print(outputdata)
    connectionSocket.close()
except IOError:
        
    connectionSocket.close()
serverSocket.close()

第4关:服务端响应请求头部信息

#import socket module
from socket import *

serverSocket = socket(AF_INET, SOCK_STREAM) 
#Prepare a sever socket 
serverSocket.bind(("127.0.0.1",6789))
serverSocket.listen(1)

#while True:
print('开始WEB服务...')

try:
        connectionSocket, addr = serverSocket.accept()
        message = connectionSocket.recv(1024) # 获取客户发送的报文
        
        #读取文件内容
        filename = message.split()[1]       
        f = open(filename[1:])
        outputdata = f.read();
        
        #发送响应的头部信息
        header = ' HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\nContent-Length: %d\n\n' % (len(outputdata))
         #########Begin#########
        connectionSocket.send(header.encode())
        #########End#########
       
        connectionSocket.close()
except IOError:
        
        connectionSocket.close()
serverSocket.close()

第5关:服务端响应请求的正文

#import socket module
from socket import *

serverSocket = socket(AF_INET, SOCK_STREAM) 
#Prepare a sever socket 
serverSocket.bind(("127.0.0.1",6789))
serverSocket.listen(1)

#while True:
print('开始WEB服务...')
try:
        connectionSocket, addr = serverSocket.accept()
        message = connectionSocket.recv(1024) # 获取客户发送的报文
        
        #读取文件内容
        filename = message.split()[1]       
        f = open(filename[1:])
        outputdata = f.read();
        
        #向套接字发送头部信息
        header = ' HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\nContent-Length: %d\n\n' % (len(outputdata))
        connectionSocket.send(header.encode())
        
        #发送文件内容
        #########Begin#########
        for _ in range(len(outputdata)):
            connectionSocket.send(outputdata[_].encode())
        #########End#########
        
        #关闭连接
        connectionSocket.close()
except IOError:             #异常处理
        #发送文件未找到的消息
        
        #关闭连接
        connectionSocket.close()
#关闭套接字
serverSocket.close()

第6关:服务端异常(文件不存在异常)处理

 #import socket module
from socket import *

serverSocket = socket(AF_INET, SOCK_STREAM) 
#Prepare a sever socket 
serverSocket.bind(("127.0.0.1",6789))
serverSocket.listen(1)

#while True:
print('开始WEB服务...')
try:
        connectionSocket, addr = serverSocket.accept()
        message = connectionSocket.recv(1024) # 获取客户发送的报文
        
        #读取文件内容
        # filename = message.split()[1]       
        # f = open(filename[1:])
        # outputdata = f.read();
        
        #向套接字发送头部信息
        # header = ' HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\nContent-Length: %d\n\n' % (len(outputdata))
        header = ' HTTP/1.1 404 not Found'
        connectionSocket.send(header.encode())

        #S发送请求文件的内容
        # for i in range(0, len(outputdata)):
        #     connectionSocket.send(outputdata[i].encode())
        
        #关闭连接
        connectionSocket.close()
except IOError:             #异常处理
        #发送文件未找到的消息
        header = ' HTTP/1.1 404 not Found'
        # header = ' HTTP/1.1 404'
        #########Begin#########
        connectionSockert.send(header.encode())
        #########End#########
        #关闭连接
        connectionSocket.close()
#关闭套接字
serverSocket.close()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值