无法理解python代码来制作服务器

时间:2018-07-07 06:43:16

标签: python sockets server

from socket import *
serverPort=15000
serverSocket = socket(AF_INET, SOCK_STREAM)

serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print 'the web server is up on port:',serverPort

while True:

 print 'Ready to serve...'
 connectionSocket, addr = serverSocket.accept()

 try:

    message = connectionSocket.recv(1024)
    print message,'::',message.split()[0],':',message.split()[1]
    filename = message.split()[1]
    print filename,'||',filename[1:]
    f = open(filename[1:])
    outputdata = f.read()
    print outputdata
    connectionSocket.send('\nHTTP/1.1 200 OK\n\n')
    connectionSocket.send(outputdata)

    for i in range(0, len(outputdata)):
        connectionSocket.send(outputdata[i])
        connectionSocket.close()

 except IOError:
    connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n')
    connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n')

我似乎不明白什么

  

对于范围(0,len(outputdata))中的i:           connectionSocket.send(outputdata [i])           connectionSocket.close()

用于。当我在for循环外放置connectionSocket.close()时,服务器将加载页面,但允许多个主机连接到服务器。 (否则,一台主机连接时服务器停止。)

而且,为什么我们要两次使用 connectionSocket.send(outputdata)?一次进入循环,一次进入循环?

0 个答案:

没有答案