#import socket modulefrom 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 modulefrom 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 connectionprint('开始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 modulefrom 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()