2015-05-19 120 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最常用的编程语言之一。 –