Python错误:缩进错误:应为缩进块

Python错误:缩进错误:应为缩进块,python,Python,我创建了一个Python脚本以连接到telnet服务器,但出现以下错误: IndentationError: expected an indented block File "./exploit_war.py", line 16 connect.connect((hostname, 21)) 脚本的一部分如下所示: #!/usr/bin/python import sys import socket hostname = sys.argv[1] username = "whateve

我创建了一个Python脚本以连接到telnet服务器,但出现以下错误:

IndentationError: expected an indented block
File "./exploit_war.py", line 16
connect.connect((hostname, 21))
脚本的一部分如下所示:

#!/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

缩进是Python中的一切:它定义了什么是循环、函数、if语句的一部分。。。您可能应该学习一些基础教程()。对于您的代码,这应该是可行的:

#!/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希望下面的行具有相同的缩进。

谢谢,它成功了。我是linux新手,我不知道这一点。不客气:-)但是,这与linux无关是的,我知道。但是Python是linux最常用的编程语言之一。但是,为什么这段代码要connect.send(“用户%s\%r\%n”%username)response=connect.recv(2000)print response connect.send(“传递%\%r\%n”%password)response=connect.recv(2000)print response不模拟回车键?