LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ValueError: invalid literal for int() with base 10: - Python (https://www.linuxquestions.org/questions/linux-newbie-8/valueerror-invalid-literal-for-int-with-base-10-python-4175593508/)

MrLinuxDonnelly 11-14-2016 08:38 AM

ValueError: invalid literal for int() with base 10: - Python
 
Hi All,

My python script appears to not like my system argv. I have a script which reads multicast data but rather than have a built in group wanted to use command line arguements.

However when i run using the command line arguements i get thrown out with the below

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

Script is below

Code:

#!/usr/bin/python

import socket
import struct
import sys

MCAST_GRP = int(sys.argv[1])
MCAST_PORT = int(sys.argv[2])

#MCAST_GRP = '226.1.1.1'
#MCAST_PORT = 11130

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((MCAST_GRP, MCAST_PORT))  # use MCAST_GRP instead of '' to listen only
#sock.bind(('', MCAST_PORT))  # use MCAST_GRP instead of '' to listen only
                            # to MCAST_GRP, not all groups on MCAST_PORT
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
  print sock.recv(10240)

Example or when running :
python watchMulticast.py 226.1.1.1 11130
Traceback (most recent call last):
File "watchMulticast.py", line 7, in <module>
MCAST_GRP = int(sys.argv[1])
ValueError: invalid literal for int() with base 10: '226.1.1.1'

If i use the built in variable works like a charm.

Any idea why it doesnt like my group im guessing it doesnt like the "."

grail 11-14-2016 10:25 AM

So you are saying that if you do the following it works just fine:
Code:

MCAST_GRP_VAL = '226.1.1.1'

MCAST_GRP = int(MCAST_GRP_VAL)

If the above works then I have zero idea.

MrLinuxDonnelly 11-15-2016 03:17 AM

Quote:

Originally Posted by grail (Post 5630465)
So you are saying that if you do the following it works just fine:
Code:

MCAST_GRP_VAL = '226.1.1.1'

MCAST_GRP = int(MCAST_GRP_VAL)

If the above works then I have zero idea.

Hey

Right so if i have the variable hard coded in the tool it works so as below :

#MCAST_GRP = '226.1.1.1'
#MCAST_PORT = 11130

Obviously no hash

However when i use it as an user input through argv it doesnt like it

MCAST_GRP = int(sys.argv[1])
MCAST_PORT = int(sys.argv[2])

Not entirely sure why it doesnt like it.

Would rather not have to change a tool each time i want to look at a different group each time.

pan64 11-15-2016 04:50 AM

MCAST_GRP = int(sys.argv[1])
why was that int put there?

MrLinuxDonnelly 11-15-2016 05:37 AM

Quote:

Originally Posted by pan64 (Post 5630743)
MCAST_GRP = int(sys.argv[1])
why was that int put there?


Was an attempt to get it to work. If int isnt there the tool dies with below so was an attempt to get it to recognise the variable as an integer


watchMulticast.py 226.1.1.1 11130
Traceback (most recent call last):
File "watchMulticast.py", line 17, in <module>
sock.bind((MCAST_GRP, MCAST_PORT)) # use MCAST_GRP instead of '' to listen only
File "<string>", line 1, in bind
TypeError: an integer is required

pan64 11-15-2016 05:41 AM

port should be an integer, but argv1 should not.

grail 11-15-2016 06:49 AM

You seemed to have missed my point. It does not matter if stored in a variable or called from the command line, but if you pass an ip address to int() you are going to get an error, hence my example
would also fail.

As has been pointed out by pan64, you need to look at what arguments are required for the call to sock.bind. One of which needs to be an integer according to your error message, but it is flawed to
think it means they both have to be integers.


All times are GMT -5. The time now is 04:25 PM.