79751102

Date: 2025-08-30 11:28:20
Score: 1
Natty:
Report link

The major problem I see here is indentation. Class methods shoud be indented inside the class to distinguish them of global functions, and code within functions must be indented too.

Quick recap of code blocks and indentation:

A code block is a set of Python code that's indented one extra level. The line just before the code block ends with a colon (:) and it contains the statement which needs the code block for it to work i.e. if, while , class , def, etc. For example, an if statement contains the if keyword itself, a condition and the code block to be executed in case the condition evaluates to True (note that if statements end with a colon to tell the Python interpreter the block is about to start), like this:

if condition:
    do_something()
    do_another_thing()
do_something_after_the_if_statement()

A code block can be inside another code block (that's called "nesting"). For that, just add an extra level of indentation in the inner code block:

print("Base level")

if True:
    print("First level")
    print("still in first level")

    if True:
        print("Second level")

        if True:
            print("Third level")

        print("Back to second level")

    print("Back to first level")

print("Back to base level")

The blank lines are just for clarification and have no syntactic utility.

In the case of function definitions, the code block indicates the code to be executed when the function is called. In class definitions, the code block is executed right away, but is only accessible to all instances of the class. Remember that class methods are functions, so code inside them must have also an extra level of indentation. A visual example:

class MyClass(object):
    def __init__(self): # the 'def' statement is in class definition (1 level)
        print("Initializing class...")
        self.a = 0
        self.b = 0
        self.c = 0 # This assignment is in method definition, which is inside class definition (2 levels)

    def a_method(self)
        self.a = 1
        self.b = 2
        self.c = 3
        if self.c == 3
            print(a + b + c) # This statement is in an if clause, which is inside a method definition, which in turn is inside a class definition (3 levels)

do_something_thats_not_in_the_class_definition()

Back to your file, your code with proper indentation would look like this:

import select
import socket
import sys
import server_bygg0203
import threading
from time import sleep

class Client(threading.Thread):

    #initializing client socket

    def __init__(self,(client,address)):

        threading.Thread.__init__(self) 
        self.client = client 
        self.address = address
        self.size = 1024
        self.client_running = False
        self.running_threads = []
        self.ClientSocketLock = None
        self.disconnected = threading.Event()

    def run(self):

        #connect to server
        self.client.connect(('localhost',50000))

        #self.client.setblocking(0)
        self.client_running = True

        #making two threads, one for receiving messages from server...
        listen = threading.Thread(target=self.listenToServer)

        #...and one for sending messages to server
        speak = threading.Thread(target=self.speakToServer)

        #not actually sure what daemon means
        listen.daemon = True
        speak.daemon = True

        #appending the threads to the thread-list
        self.running_threads.append((listen,"listen"))
        self.running_threads.append((speak, "speak"))
        listen.start()
        speak.start()

        while self.client_running:

            #check if event is set, and if it is
            #set while statement to false

            if self.disconnected.isSet():
                self.client_running = False 

        #closing the threads if the client goes down
        print("Client operating on its own")
        self.client.shutdown(1)
        self.client.close()
        
        #close threads

        #the script hangs at the for-loop below, and
        #refuses to close the listen-thread (and possibly
        #also the speak thread, but it never gets that far)

        for t in self.running_threads:
            print "Waiting for " + t[1] + " to close..."
            t[0].join()
        self.disconnected.clear()
        return

    #defining "speak"-function      
    def speakToServer(self):

        #sends strings to server
        while self.client_running:
            try:
                send_data = sys.stdin.readline()
                self.client.send(send_data)

                #I want the "close" command
                #to set an event flag, which is being read by all other threads,
                #and, at the same time set the while statement to false

                if send_data == "close\n":
                    print("Disconnecting...")
                    self.disconnected.set()
                    self.client_running = False
            except socket.error, (value,message):
                continue
        return

    #defining "listen"-function
    def listenToServer(self):

        #receives strings from server
        while self.client_running:

            #check if event is set, and if it is
            #set while statement to false

            if self.disconnected.isSet():
                self.client_running = False
            try:
                data_recvd = self.client.recv(self.size)
                print data_recvd
            except socket.error, (value,message):
                continue
        return

if __name__ == "__main__":
    c = Client((socket.socket(socket.AF_INET, socket.SOCK_STREAM),'localhost'))
    c.run()
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: guest