Python Forum
ValueError: invalid literal for int() with base 10:
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: invalid literal for int() with base 10:
#1
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
Reply
#2
Quote:his error "ValueError: invalid literal for int() with base 10: 'terertsagdx'"......
Please show complete unmodified error traceback as it contains valuable diagnosis information.
Reply
#3
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')
Reply
#4
ascii on line 19 doesn't seem to be declared anywhere, so I'd expect to see a NameError.
Reply
#5
ascii is a built-in function, so it's already in the namespace without an assignment.
Reply
#6
(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".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  invalid literal for int() with base 10: '# NRECS: 1096\n' Baloch 8 4,537 May-24-2020, 02:08 AM
Last Post: Larz60+
  invalid literal for int() with base 10: '' mrsenorchuck 5 5,425 Apr-29-2020, 05:48 AM
Last Post: markfilan
  ValueError: invalid literal for int() with base 10: '\n' srisrinu 9 5,715 Apr-13-2020, 01:30 PM
Last Post: ibreeden
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,824 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  input-ValueError: invalid literal for int() jacklee26 2 2,544 Feb-21-2020, 01:27 PM
Last Post: ndc85430
  ValueError: invalid literal for int() with base 10: '0.5' emmapaw24 2 3,730 Feb-16-2020, 07:24 PM
Last Post: emmapaw24
  ValueError: invalid literal for int() with base 10: '' Jay123 7 7,287 Aug-05-2019, 02:43 PM
Last Post: Jay123
  ValueError: invalid rectstyle object fen1c5 1 5,671 Jun-05-2019, 02:51 PM
Last Post: heiner55
  ValueError: invalid literal for int() with base 10: '' ivinjjunior 6 9,144 Apr-20-2019, 05:37 PM
Last Post: keames
  Problem with "invalid literal for int() with base 10: '' jirkaj4 4 10,445 Jan-23-2018, 06:55 PM
Last Post: jirkaj4

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020