Re: Try: Except: evaluates to True every time

2017-11-04 Thread Karsten Hilbert
On Sat, Nov 04, 2017 at 05:07:26PM +0100, Karsten Hilbert wrote:

> Try in an interactive interpreter:
> 
>python> "a string" is True

Or, rather,

python> if 'a string': print 'success'

Sorry,
Karsten
-- 
GPG key ID E4071346 @ eu.pool.sks-keyservers.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Try: Except: evaluates to True every time

2017-11-04 Thread Steve D'Aprano
On Sun, 5 Nov 2017 02:31 am, brandon wallace wrote:

> 
> I have this code that tests a server to see if it is listening on port 123
> runs and evaluates to True every time. Even if the server does not exist but
> it is not supposed to do that. I am getting no error message at all. What is
> going on with this code?

You're returning a string in both cases. Both strings evaluate as true when
treated as bools.

Success:

> return "Port 53 is reachable on: %s" % host

Failure:

> except socket.error as e:
> return "Error on connect: %s" % e
> 
> check_udp(hostname, port)


That's the first bug. The second bug is that I don't think the code does what
you think it does. You seem to be calling it with a single hostname,
presumably a string. But then you split the hostname into individual letters,
and try to connect to each of them. The *first* attempt either succeeds or
fails, and then returns.

So if you call check_udp("mailserver", 143), your function calls

for host in "mailserver":
try:
s.connect((host, port_num))


which attempts to connect to ("m", 143). That will either succeed (probably
not), or fail (probably this), and then the function returns a string, which
you apparently never look at.

I suggest you re-write your check_udp function to something more like this:


def check_udp(host, port_num):
'''Test the UDP port on a remove server.'''
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect((host, port_num))
return True
except socket.error as e:
return False


But even this is suspicious, since it is vulnerable to a "Time Of Check To
Time Of Use" bug. Just because you can connect to the host *now*, when you
call check_udp, doesn't mean it will still respond two seconds later (or even
two milliseconds later) when you attempt to connect again.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Try: Except: evaluates to True every time

2017-11-04 Thread Chris Angelico
On Sun, Nov 5, 2017 at 2:31 AM, brandon wallace  wrote:
>
> I have this code that tests a server to see if it is listening on port 123 
> runs and evaluates to True every time. Even if the server does not exist but 
> it is not supposed to do that. I am getting no error message at all. What is 
> going on with this code?
>
>
>
> #!/usr/bin/env python
>
> import socket
>
> hostname = ["192.168.1.22", "192.168.1.23", "200.168.1.24", "19.0.0.0"]
> port = 123
>
> def check_udp(hosts, port_num):
> '''Test the UDP port on a remove server.'''
> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> for host in hosts:
> try:
> s.connect((host, port_num))
> return "Port 53 is reachable on: %s" % host
> except socket.error as e:
> return "Error on connect: %s" % e
>
> check_udp(hostname, port)

Do you understand what it actually means to connect a UDP socket? If
not, I suggest reading up on the nature of UDP. You can't probe a
remote server this way; it simply doesn't work like that.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list