0

this is the strangest error i ever had (i write this because most of my post is code!! can you help me? i have a new error :/ line 43: conn.send = command.encode() NameError: name 'conn' is not defined here's the code:

import os
import socket
import sys
from _thread import *
mm = 0
owncmds = ["dir", "erase"]
def clientthread(conn):
    buffer = ""
    data = conn.recv(8192)
    buffer += data
    print(buffer)
    # conn.sendall(reply)
def main():
    try:
        host = socket.gethostname()
        port = 6666
        tot_socket = input("Wie viele Clients sind zugelassen?: ")
        list_sock = []
        for i in range(int(tot_socket)):
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.bind((host, port + i))
            s.listen(2)
            list_sock.append(s)
            print("[*] Server listening on %s %d" % (host, (port + i)))
        for j in range(len(list_sock)):
            conn, addr = list_sock[j].accept()
            print('[*] Connected with ' + addr[0] + ':' + str(addr[1]))
            start_new_thread(clientthread, (conn,))
    finally:
        s.close()
main()
while mm < 1:
    command = input(str("Command: "))
    if command not in owncmds:
        conn.send(command.encode())
    else:
        if command == "dir":
            result = conn.recv(1024)
            result = result.decode()
            print(result)
        if command == "erase":
            command = command + "/F /Q "
            FileErase = input(str("Filename: "))
            command = command + FileErase
            conn.send(command.encode())
    print("Der Befehl wurde gesendet, warte auf Akzeptierung")
    print("")
Jongware
  • 22,200
  • 8
  • 54
  • 100
Rysed
  • 1
  • 1
    `conn` is defined inside a function and so it's forgotten as soon as the function ends. – Jongware Feb 29 '20 at 17:14
  • "This is the strangest error I ever had". You've had very few errors, then. – chepner Feb 29 '20 at 17:22
  • bro i literally wrote, that im writing "This is the strangest error I ever had" only because else, i couldn't post it. – Rysed Mar 01 '20 at 18:08
  • @usr2564301 do you have any solutions? – Rysed Mar 01 '20 at 18:14
  • Possible duplicate of [My variable is defined but python is saying it isn't?](https://stackoverflow.com/questions/39393789/my-variable-is-defined-but-python-is-saying-it-isnt) – Jongware Mar 01 '20 at 18:22

0 Answers0