尝试后的Python缩进错误:

时间:2013-05-18 22:56:13

标签: python runtime-error indentation

每当我尝试在Raspberry PI上运行此python脚本时,我都会遇到问题:

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(16)
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                connection.sendall(data)
            else:
                print >>sys.stderr, 'no more data from', client_address
                break

    finally:
        # Clean up the connection
        connection.close()

我收到此错误:

 File "server.py", line 20
    try:
    ^
IndentationError: unexpected indent
你能告诉我这里有什么问题吗?该脚本应该创建一个简单的TCP / IP服务器,我对客户端没有这样的问题,所以我真的不明白我的错误在哪里...

1 个答案:

答案 0 :(得分:3)

Python使用空格表示块的一个令人遗憾的副作用是,有时你会得到在整个源代码中混合了标签和空格的脚本。

由于此脚本非常小,您可以尝试删除每行代码前面的空格,然后重新加入它。