【发布时间】:2018-09-10 12:57:00
【问题描述】:
我目前正在学习 python 一个月,因为我想为我的 GameMakerStudio 2 游戏编写一个服务器,它不支持线程,我需要一个用另一种语言编写的服务器才能使其可用。 无论如何... https://www.youtube.com/watch?v=WrtebUkUssc -教程网址
我正在关注本教程,不幸的是我收到了一个错误:
/home/borut/PycharmProjects/Server/venv/bin/python /home/borut/PycharmProjects/Server/Server.py 文件“/home/borut/PycharmProjects/Server/Server.py”,第 19 行 而真: ^ IndentationError: 意外缩进
进程以退出代码 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,))
感谢您的所有回答和提示!
【问题讨论】:
-
你的缩进看起来是随机的。这将激怒 Python 之神要求在这方面保持一致。 stackoverflow.com/questions/1024435/…
标签: python networking