2015-05-19 117 views
-1

我創建了一個Python腳本連接到telnet服務器,我得到這個錯誤:Python的錯誤:IndentationError:預計縮進塊

IndentationError: expected an indented block 
File "./exploit_war.py", line 16 
connect.connect((hostname, 21)) 

腳本的一部分follwoing:

#!/usr/bin/python 

import sys 
import socket 


hostname = sys.argv[1] 
username = "whatever" 


connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

try: 

connect.connect((hostname, 21)) 


except: 


print "[-] connection error" 
response = connect.recv(2000) 
print response 
sys.exit(1) 

有人可以幫我嗎?

那將是非常好的..

感謝

編輯:爲什麼這部分代碼沒有模擬輸入鍵?:

connect.send("user %s\r\n" %username) 
response = connect.recv(2000) 
print response 



connect.send("pass %s\r\n" %password) 
response = connect.recv(2000) 
print response 

回答

3

縮進一切在Python:它定義了什麼是循環,函數,if語句的一部分......你應該通過一些基本教程(https://docs.python.org/2/tutorial/)。對於你的代碼,這應該工作:

#!/usr/bin/python 

import sys 
import socket 

hostname = sys.argv[1] ## see how i changed the indentation of these two lines 
username = "whatever" 

connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

try: 
    connect.connect((hostname, 21)) ## I also changed the indentation here 
except: 
    print "[-] connection error" ## ... and here 

response = connect.recv(2000) # this line will raise an error if the connection attempt fails 
print response 
sys.exit(1) 

在你的代碼,該行hostname = ...username = ...始於一個空間,他們縮進。 Python期望以下行具有相同的縮進。

+0

謝謝,它的工作。我是Linux新手,我不知道這一點。 –

+2

不客氣:-)但是,這與linux無關 –

+0

是的,我知道。但是Python是Linux最常用的編程語言之一。 –

相關問題