ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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???
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.
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. 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")
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.