LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2021, 08:50 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 889

Rep: Reputation: 41
Python - SyntaxError: invalid syntax


I am trying to reuse following python code to check host and port connectivity.
https://gist.github.com/betrcode/0248f0fda894013382d7
Code:
import socket
def isOpen("192.168.56.104",22):
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   try:
      s.connect((ip, int(port)))
      s.shutdown(2)
      return True
   except:
      return False
The problem is that it does not work at all and its hard to me to figure out what is the issue (i just started with python today).
Seems like it does not like quota. But no matter what I try - change to single quota or remove quota completely - the error stays the same
Code:
./con.py
  File "./con.py", line 11
    def isOpen("192.168.56.104",22):
                              ^
SyntaxError: invalid syntax
 
Old 05-07-2021, 08:53 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
you need to specify variable names in functions, like:
Code:
def isOpen(ip, port):
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   try:
      s.connect((ip, int(port)))
      s.shutdown(2)
      return True
   except:
      return False

a = isOpen("192.168.56.104",22)
Or probably do you want to specify default values?
Code:
def isOpen(ip="default ip", port="default port"):
 
Old 05-07-2021, 10:21 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 889

Original Poster
Rep: Reputation: 41
Thanks. I guess it worked... I was expecting to get 0 or 1 (True or False actually) but instead i got... nothing?
Why?

Another question:
How come it knows what "a" is? It is nowhere used in the code. Only at the last line of the code.
I guess def isOpen(ip, port): somehow matches what stands in "a"... but how does it know to look for values there???
 
Old 05-07-2021, 10:31 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
Quote:
Originally Posted by czezz View Post
Thanks. I guess it worked... I was expecting to get 0 or 1 (True or False actually) but instead i got... nothing?
Sorry, I don't really understand what did you expect and why. [obviously] the funtion will/should return with True or False (not 0 or 1), but you need to do something with that return value otherwise it will be simply lost.
Quote:
Originally Posted by czezz View Post
Why?
https://www.programiz.com/python-programming/function

Quote:
Originally Posted by czezz View Post
Another question:
How come it knows what "a" is? It is nowhere used in the code. Only at the last line of the code.
I guess def isOpen(ip, port): somehow matches what stands in "a"... but how does it know to look for values there???
a is a variable and your function will return with some value (in your case True or False) and that value will be assigned to a.
you can simply use
Code:
print(a)
to print the content of a (if a was a variable, not an object).
 
1 members found this post helpful.
Old 05-07-2021, 12:43 PM   #5
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 889

Original Poster
Rep: Reputation: 41
OK, im slowly getting there

1. def isOpen(ip, port) - is the definition/structure/body of the function isOpen()
2. isOpen() is executed with parameters - in this example: "192.168.56.104",22
3. a is variable that holds the output of the executed function isOpen()
At least this is how I explain it to myself

4. Now a mystery is why in the if() I cannot use string 'True' but on the other hand 1 / 0 works

Code:
#!/usr/bin/python3

import socket
def isOpen(ip, port):
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   try:
      s.connect((ip, int(port)))
      s.shutdown(2)
      return True
   except:
      return False

a = isOpen("192.168.56.104",22)
print(a)

if (a == 1):
   print("Host is UP 1")

if (a is 'True'):   <<<--- This seems to be ignored
   print("Host is UP True")
 
Old 05-07-2021, 12:47 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,170
Blog Entries: 1

Rep: Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536
True and 'True' aren't the same, don't expect them to be equal. Also it is '==' not 'is':
Code:
if (a==True):
    print("Connecting succeed");
 
1 members found this post helpful.
Old 05-07-2021, 12:50 PM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,339

Rep: Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133

You're trying to compare a boolean to a string, and even correcting "'True'" to "True" is redundant because you already have a boolean.

All you need is "if a:", or do away with the single-use variable and use "if isOpen("192.168.56.104",22):"


Last edited by boughtonp; 05-07-2021 at 12:52 PM.
 
2 members found this post helpful.
Old 05-07-2021, 01:41 PM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
Quote:
Originally Posted by boughtonp View Post
You're trying to compare a boolean to a string, and even correcting "'True'" to "True" is redundant because you already have a boolean.
Code:
a=True
if a == True:
    print("ok")   # works
if a == "True":
   print("ok")    # not ok, compare a string to boolean
if a:
    print("ok")   # works
 
1 members found this post helpful.
  


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
Green-Recorder "SyntaxError: invalid syntax" when executing the binary, python 2.7, Fedora 32 TheJooomes Linux - Software 5 05-20-2020 01:53 PM
SyntaxError: Invalid Syntax NewSheriff Linux - Newbie 3 02-19-2018 06:51 PM
[SOLVED] Python Newbie: SyntaxError: invalid syntax gdizzle Programming 1 07-08-2014 07:44 PM
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
Trouble with SyntaxError Gerardoj Programming 6 12-24-2003 06:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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
Open Source Consulting | Domain Registration