Python Forum
ValueError: invalid literal for int() with base 10: - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: invalid literal for int() with base 10: (/thread-27554.html)



ValueError: invalid literal for int() with base 10: - omega_elite - Jun-10-2020

Hi

I am new to python and am playing with some code that I got on an online course but am getting this error "ValueError: invalid literal for int() with base 10: 'terertsagdx'"...... I am connecting to the server with realterm and sending 'terertsagdx, as ascii but get the error and am not sure why ????

import socket 
import threading

HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(conn, addr):
    print(f"[NEW CONNECTION] {addr} connected.")

    connected = True
    while connected:
            msg = conn.recv(1024).decode(ascii)
            print(msg)
            conn.send("Msg received")

    conn.close()
        

def start():
    server.listen()
    print(f"[LISTENING] Server is listening on {SERVER}")
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")


print("[STARTING] server is starting...")
start()
Thanks for any help


RE: ValueError: invalid literal for int() with base 10: - Larz60+ - Jun-10-2020

Quote:his error "ValueError: invalid literal for int() with base 10: 'terertsagdx'"......
Please show complete unmodified error traceback as it contains valuable diagnosis information.


RE: ValueError: invalid literal for int() with base 10: - bowlofred - Jun-10-2020

On line 19 you may have meant to use decode('ascii') or just decode(). Passing in the ascii object is not correct.

Likewise on line 21, you're handing a string object to send(). You want to either encode that first, or just pass a byte object directly like send(b'Msg received')


RE: ValueError: invalid literal for int() with base 10: - ndc85430 - Jun-11-2020

ascii on line 19 doesn't seem to be declared anywhere, so I'd expect to see a NameError.


RE: ValueError: invalid literal for int() with base 10: - bowlofred - Jun-11-2020

ascii is a built-in function, so it's already in the namespace without an assignment.


RE: ValueError: invalid literal for int() with base 10: - delonbest - Dec-30-2020

(Jun-10-2020, 07:59 PM)omega_elite Wrote: ValueError: invalid literal for int() with base 10

The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit. You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False . The other way to overcome this issue is to wrap your code inside a Python try...except block to handle this error.

Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 . With Python2.x , int(str(3/2)) gives you "1". With Python3.x , the same gives you ("1.5"): ValueError: invalid literal for int() with base 10: "1.5".