LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2016, 08:38 AM   #1
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Rep: Reputation: Disabled
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 "."
 
Old 11-14-2016, 10:25 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
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.
 
Old 11-15-2016, 03:17 AM   #3
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 11-15-2016, 04:50 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,083

Rep: Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141
MCAST_GRP = int(sys.argv[1])
why was that int put there?
 
Old 11-15-2016, 05:37 AM   #5
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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
 
Old 11-15-2016, 05:41 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,083

Rep: Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141Reputation: 4141
port should be an integer, but argv1 should not.
 
Old 11-15-2016, 06:49 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ValueError: Entry points must be listed in groups (Python compilation error) Bindestreck Slackware 3 08-02-2016 07:16 AM
[SOLVED] Python Installed; bash command: python base.py. gets syntax error in base.py cre84j Linux - Software 4 01-23-2011 04:46 PM
invalid conversion from 'int' to 'unsigned int*' tigerhp Programming 2 03-02-2008 04:21 PM
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 08:30 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration