Posts: 1
Joined: Wed Jun 10, 2020 9:44 pm

ValueError: invalid literal for int() with base 10: 'test'

Wed Jun 10, 2020 9:50 pm


I am getting the error below when running the following code and i send data "test" from realterm on a windows PC to the server using code below and and dont know why ???

Code: Select all

import socket 
import threading

PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())


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

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

    connected = True
    while connected:
            message = conn.recv(1024)
            conn.send("Msg received")


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

print("[STARTING] server is starting...")
Exception in thread Thread-29:
Traceback (most recent call last):
File "C:\Users\Andy\AppData\Local\Programs\Python\Python38-32\lib\", line 932, in _bootstrap_inner
File "C:\Users\Andy\AppData\Local\Programs\Python\Python38-32\lib\", line 870, in run
self._target(*self._args, **self._kwargs)
File "c:/Python Code/server", line 21, in handle_client
ValueError: invalid literal for int() with base 10: 'test'

Posts: 1598
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: ValueError: invalid literal for int() with base 10: 'test'

Thu Jun 11, 2020 4:53 am

When I run this code with python3, the error message received is:

Code: Select all

Exception in thread Thread-1:
Traceback (most recent call last):
  File "... src/", line 21, in handle_client
    conn.send("Msg received")
TypeError: a bytes-like object is required, not 'str'
which is caused by the line "conn.send("Msg received")", should be "conn.send( b"Msg received")".

Looks as if the error message "ValueError: invalid literal for int() with base 10: 'test'" is not caused by this code snippet.

When I try to convert the received data to an int, like:
then the same error message is thrown when input is e.g. "hello", sent by remote client.

Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: ValueError: invalid literal for int() with base 10: 'test'

Thu Jun 11, 2020 9:59 pm

I think that you need to look up the syntax for message.decode(). It looks to me as if Python is (not unreasonably) telling you that it can't convert an alpha string into an int.

Return to “Python”