我需要帮助弄清楚我的proffesor的这个程序是如何工作的

时间:2017-11-14 02:54:02

标签: python-3.x

我已经分配了一个程序,我们需要编辑部分内容,但对于我的生活,我不能让它首先工作。我知道这只是我的愚蠢,所以我希望有人可以帮助我。

我知道它可以通过网络浏览器工作,但我无法弄清楚如何工作,因为我没有看到任何输入或类似的东西。

    # -------------------------------------------------------------------------------
# Name:        Hello World Server
# Purpose:     ¯\_(ツ)_/¯
#
# Author:      JaredRand
#
# Created:     11/1/17
# Copyright:
# Licence:
# -------------------------------------------------------------------------------

from socket import *


def main():
    serverPort = 8080
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind(('localhost', serverPort))
    serverSocket.listen(0)  # number of backlogged connections
    print('server ready')
    while 1:

        try:
            connectionSocket, addr = serverSocket.accept()  # make everything after this a function?
            print('passed try 1')
        except IOError:
            print("Server Socket Accept Error")

        try:
            request = connectionSocket.recv(1024).decode('utf-8')
            print('passed try 2')
            print(request)
        except IOError:
            print("Server Socket Recv Error")

        if request:
            # https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
            try:

                [Method, Request_URI, HHTP_Version] = request.split(' ', 2)
                print('passed try 3')
                print(Method)
                print(Request_URI)
                print(HHTP_Version)
            except ValueError:
                print("Request Parse Error:" + request)

            try:
                # https://www.ietf.org/rfc/rfc2396.txt
                [scheme, hier_part] = Request_URI.split(":", 1)
                print('passed try 4')
                print(scheme)
                print(hier_part)
            except ValueError:
                print("No Scheme")
                scheme = None
                hier_part = Request_URI

            # more parsing is required but assuming the Request_URI is a path
            print("Request URI is: " + hier_part)

            # see if the file is present
            if hier_part != "/":
                try:
                    print("Request File is: " + hier_part)
                    fo = open('.' + hier_part, "rb")
                except IOError:
                    # here need to send a 404 error
                    http_status = 'HTTP/1.1 404 Not Found\n'
                    http_content = 'Content-Type: text/html charset=utf-8\n\n'
                    outputdata = 'Bad File'
                else:
                    # right now only file we have is the icon
                    outputdata = fo.read()
                    fo.close()
                    http_status = 'HTTP/1.1 200 OK\n'
                    http_content = 'Content-Type: image/x-icon\n\n'
            else:
                # here we would the contents of index.html
                outputdata = '<!DOCTYPE html><head><meta charset="utf-8">' \
                             + '<title> test </title></head><body><h1>Index File</h1><p>Should be index</p></body></html>'
                http_status = 'HTTP/1.1 200 OK\n'
                http_content = 'Content-Type: text/html charset=utf-8\n\n'

            # send the response header

            connectionSocket.send(http_status.encode('utf-8'))
            connectionSocket.send('Connection: close\n'.encode('utf-8'))
            # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Should
            LengthString = 'Content-Length: ' + str(len(outputdata)) + '\n'
            # connectionSocket.send('Transfer-Encoding: identity\n')
            connectionSocket.send(LengthString.encode('utf-8'))
            connectionSocket.send(http_content.encode('utf-8'))

            print(type(outputdata))
            try:
                outputdatae = outputdata.encode('utf-8')
            except AttributeError:
                outputdatae = outputdata

            connectionSocket.send(outputdatae)

            connectionSocket.shutdown(SHUT_RDWR)
            connectionSocket.close()
        else:
            print("No Request")

    pass


if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:1)

这是一个启动Web服务器的Python脚本。

  1. 为您的操作系统安装Python(如果需要)
  2. 将文件另存为webserver.py
  3. 运行Python脚本python webserver.py
  4. 打开网络浏览器并转到http://localhost:8080/

答案 1 :(得分:0)

运行正常,运行程序然后转到the port,您应该看到This site