0

In the code below, I am attempting to decrypt a padded AES piece of text sent over TCP to my server.py file from a separate file named "client.py":

def handle(client):
    while True:
        try:
            message = client.recv(1026)
            key_iv = AES.new(b'5TGB&YHN7UJM(IK<', AES.MODE_CBC, b'!QAZ2WSX#EDC4RFV')
            decrypt1 = key_iv.decrypt(message)
            decrypt2 = decrypt1[0:len(text)]
            broadcast(decrypt2)
            time.sleep(2)

To do this, I must import the variable "text" from client.py via 'from client import text'. Though, after I do this and then attempt to start up my server, I am shown a error that is only specific on the client file. The error shows 'Connection timed out, probably host is down' and the code to show such a message is only present in the client.py file (shown below):

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP = '192.168.68.166'
PORT = 4444
try:
    client.connect((IP, PORT))
    username = input("Username: ")
except socket.error:
    print("Connection timed out, probably host is down")
    client.close()
    sys.exit()

I know for a fact I am not experiencing socket errors when starting up my server and - even if I was - I should not be being shown an error from a piece of code I never imported

martineau
  • 119,623
  • 25
  • 170
  • 301
SQLi
  • 13
  • 2
  • 1
    I get the feeling that you have code in `client.py` which is not wrapped in an `if __name__ == '__main__':`, meaning that that code runs whenever you import anything from that file. Putting code in the base scope of the file means that that code will be executed when the file is interpreted. I can't be sure since you didn't provide a [mre] but I can't really imagine there being another reason. – Random Davis Oct 20 '21 at 18:44
  • 1
    Importing anything from `client` runs `client.py`. If you want to prevent that when importing it as a module, use `if __name__ == __main__:` for the parts that shouldn't run when imported. – Thierry Lathuille Oct 20 '21 at 18:44
  • I think the key misunderstanding here is what `from client import text` does. It runs ALL the code at the base level of `client.py` and then brings in `text` (whatever it may be) into your current scripts namespace. – Axe319 Oct 20 '21 at 18:49

0 Answers0