2017-08-23 7 views
-1

プログラミングに慣れていないため、検出できなかったコードには間違いがある可能性があります。でる。私はシンプルなWebサーバーを実装しようとしていますが、ヘッダー行の前に要求されたファイルで構成されたHTTP応答メッセージをサーバーが作成する部分にインデントエラーが発生し続けます。「インデントエラー - ヘッダーを扱うときにインデントブロックが必要です」

from socket import * 
import sys 

host = 'localhost' 
serverPort = int(sys.argv[1]) 
serverSocket = socket(AF_INET, SOCK_STREAM) 
serverSocket.bind((host,serverPort)) 
serverSocket.listen(1) 

while True: 
    connectionSocket, addr = serverSocket.accept() 

    try: 
     #if received correct stuff 
     File_Wanted = connectionSocket.recv(1024) 
     File_Name = File_Wanted.split()[1] 
     #open and read line in index.htm 
     File = open('index.htm','r') 
     output = File.read() 
     #should I close it here or somewhere else? 
     File.close() 

     #take care of header first 
     connectionSocket.send('HTTP/1.0 200 OK\r\n') 

     #looks good, then send to client(browser) 
     for i in range(0, len(output)): 
     connectionSocket.send(output[i]) 
     connectionSocket.close() 


    IOError: 
    #Send response message for file not found 
     connectionSocket.send('404 Not Found')      
    #Close client socket  
     connectionSocket.close()        


serverSocket.close() 

私は崇高な上に構築すると、私は得た。

connectionSocket.send(output[i]) 
^ 

IndentationError:

+3

Pythonはwを使用します。ブロックを定義するための空白を入れるには、 'for'文の後に行をインデントする必要があります。 – emredjan

+0

serverSocket.close()の後に字下げをしてください。 – Oqhax

+0

' for'にある 'for'ループの後ろに字下げ行を入れてください。 – WhatsThePoint

答えて

0

Pythonはブロックを定義するための空白文字を使用していますidentedブロックを期待して、あなたはfor文の後の行をインデントする必要があります。

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

、あなたがチェックする前にexcept文を必要としますエラー:

except IOError: 
関連する問題