-4

I am learning python currently for a month now because I want to write a server for my GameMakerStudio 2 game, which doesn't support threading and I need a server to be written in another language to make this available. Anyways... https://www.youtube.com/watch?v=WrtebUkUssc -tutorial url

I was following this tutorial and unfortunately i get an error which is:

/home/borut/PycharmProjects/Server/venv/bin/python /home/borut/PycharmProjects/Server/Server.py File "/home/borut/PycharmProjects/Server/Server.py", line 19 while True: ^ IndentationError: unexpected indent

Process finished with exit code 1

And my code as followed on tutorial is:

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,))

thank you for all your answers and tips guys!

  • 5
    You indentation appears random. This will anger the Python Gods would demand uniformity in this regard. https://stackoverflow.com/questions/1024435/how-to-fix-python-indentation – Stephen Rauch Mar 31 '18 at 20:33

2 Answers2

1

The issue is that your indentation is incorrect. By indentation I mean

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 is helpful for you to see why indentation is important.

To fix this, format the code with an IDE. You can use this code instead, I have fixed indentation for you.

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,))
halfer
  • 19,824
  • 17
  • 99
  • 186
toheedNiaz
  • 1,435
  • 1
  • 10
  • 14
1

Indentation is the most important think for Python since is the way you inform where begin/end a block of code.

Your first

while True:

is one tab ahead

Hasher
  • 62
  • 1
  • 2
  • 8