Aw: Try: Except: evaluates to True every time

Karsten Hilbert Karsten.Hilbert at gmx.net
Sat Nov 4 12:07:26 EDT 2017


Try in an interactive interpreter:

   python> "a string" is True

Karsten

> Gesendet: Samstag, 04. November 2017 um 16:31 Uhr
> Von: "brandon wallace" <nodnarb at gmx.us>
> An: python-list at python.org
> Betreff: Try: Except: evaluates to True every time
>
> 
> 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)
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list