Python,而真正的wierd

2024-04-20 12:39:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在学习python已经一个月了,因为我想为我的GameMakerstudio2游戏编写一个服务器,它不支持线程,我需要用另一种语言编写一个服务器来实现这一点。 不管怎样。。。 https://www.youtube.com/watch?v=WrtebUkUssc-教程url

我遵循了本教程,不幸的是,我得到一个错误是:

/home/borut/pycharm项目/Server/venv/bin/python/home/borut/pycharm项目/Server/服务器.py 文件“/home/borut/PycharmProjects/Server”/服务器.py“,第19行 如果为真: ^ 缩进错误:意外缩进

进程已完成,退出代码为1

我的代码如下:

import socket
import sys
from _thread import *

host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((host, port))
except socket.error as e:
    print(str(e))

s.listen(5)
print ('waiting for a connection')
def threaded_client(conn):
    conn.send(str.encode('Welcome, type your info\n'))

        while True:
                data = conn.recv(2048)
                reply = 'Server output: ' +data.decode('utf-8')
                if not data:
                    break;
                 conn.sendall(str.encode(reply))
                conn.close()

while True:
                conn, addr = s.accept()
                print('connected to: '+addr[0]+':'+str(addr[1]))

                start_new_thread(threaded_client, (conn,))

谢谢大家的回答和提示!你知道吗


Tags: 项目import服务器homedataserver错误教程
2条回答

问题是你的缩进不正确。我是说缩进

Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements

This link有助于您了解缩进的重要性。你知道吗

要解决此问题,请使用IDE格式化代码。你可以用这个代码代替,我为你修正了缩进。你知道吗

import socket
import sys
from _thread import *

host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((host, port))
except socket.error as e:
    print(str(e))

s.listen(5)
print ('waiting for a connection')
def threaded_client(conn):
    conn.send(str.encode('Welcome, type your info\n'))

    while True:
        data = conn.recv(2048)
        reply = 'Server output: ' +data.decode('utf-8')
        if not data:
             break
        conn.sendall(str.encode(reply))
        conn.close()

while True:
        conn, addr = s.accept()
        print('connected to: '+addr[0]+':'+str(addr[1]))

        start_new_thread(threaded_client, (conn,))

缩进是Python最重要的思想,因为缩进是通知代码块的开始/结束位置的方式。你知道吗

你的第一个

while True:

前面有一张票吗

相关问题 更多 >