缩进块错误Python

时间:2014-09-17 17:27:47

标签: python sockets python-2.7 compiler-errors

from socket import *
def main():

    serverPort=28282
    serverSocket = socket(AF_INET, SOCK_STREAM)
    #Prepare a server socket
    #fill in start
    serverSocket.bind(('localhost',serverPort))
    serverSocket.listen(1)
    #fill in end
    while True:
    #Establish the connection
    print 'Ready to serve...'
    connectionSocket, addr = serverSocket.accept()
    try:
    message = connectionSocket.recv(1024)
    filename=message.split()[1]
    f=open(filename[1:])
    outputdata=f.read()

    print (outputdata)
        connectionSocket.sent('\nHttp/1.1 200 ok\n\n\n\n')     
        connectionSocket.send(outputdata[i])

    connectionSocket.close()        
    except (IOError) as e:

    pass
        print('404 Page not found')
        connectionSocket.send('\Http/ 1.1 404 Not found\n ')

    serverSocket.close()

这是我的代码,当我运行文件时它给我意图阻止错误,我不知道有什么问题,有人请帮助我吗?

1 个答案:

答案 0 :(得分:1)

我不确定您在此处发布代码时是否丢失了格式,但while语句下的任何内容都没有缩进,tryexcept块下也没有

Python非常讲究缩进,就是它如何在流控制结构的上下文中确定作用域。

正确

if x == 1:
    print True
else:
    print False

不正确

if x == 1:
print True
else:
print False