Python Forum

Full Version: try: and except:
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
   I been slowly making my proxy site tester better. It works well now. One of the things I want to do is make it print out that the proxy it down. Right now if the proxy is down you get an error, that it timed out. The way that I can change that is with try and except.
I been reading about how to use it, and watch many videos.
   The problem I am have is I can find and error that will work.
here is the code so far:

import socket # for socket
import sys
print"Proxy tester"
print"By Blue Dog"
host_ip = input("type in the proxy address ")
port = input("type in the port number")
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print "Socket successfully created"
except socket.error as err:
    print "socket creation failed with error %s" %(err)

try:
    host_ip = socket.gethostbyname(host_ip)
    
except 10060:
    print "The Proxy Host Is Down"
    print "there was an error resolving the host"
    sys.exit()

s.connect((host_ip,port))
print "on port"
print (port)
print "the socket has successfully connected to proxy %s" %(host_ip)

input("Press <enter> to close.")
Here is the error I get when it time out.
Error:
Traceback (most recent call last):   File "C:\Users\renny and kite\Desktop\sock_proxy_2.py", line 21, in <module>     s.connect((host_ip,port))   File "C:\Python27\lib\socket.py", line 228, in meth     return getattr(self._sock,name)(*args) error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I don't want this error to come up, I just want it to print out Proxy is down.
I have try all the error that is in this error statement, none works.
I hope I made myself clear.
s.connect((host_ip,port)) is not in a "try block" in your code....

except takes an exception type (it can be Exception, the base class of all Exception). Something like except 10060 won't work.
Hello!
You can set the timeout before the connection.

socket.settimeout(6)

socket.connect(url)
Also, see socket.connect_ex()
Hey, Ofnuts, I know it does not work, that is why I am here. I want to find out what does work. Right now is I want to get rid of the error and have print "The Proxy Host Is Down" come up and then it exit.
So can anyone tell what I should use to keep the time out error from not coming up when it time out?
You can use the following in your except clause to print all unknown errors:
print("Unexpected error:", sys.exc_info()[0])
To use, you must import sys
(Feb-06-2017, 12:05 AM)Blue Dog Wrote: [ -> ]Hey, Ofnuts, I know it does not work, that is why I am here. I want to find out what does work. Right now is I want to get rid of the error and have print "The Proxy Host Is Down" come up and then it exit.
So can anyone tell what I should use to keep the time out error from not coming up when it time out?

When you do a try/except the (optional) name after except is an exception type. Any exception which is of that type or of a derived type is caught there (unless it is caught by a previous and more specific except).  So if you want to catch all kind of errors, you specify a rather generic type, usually Exception. You can also specify nothing, but this gets dangerous since it can lead to ignore syntax errors or missing variables...
Ofnuts, I tryed it, but still the time out error came up.
Thank you
(Feb-06-2017, 02:49 PM)Blue Dog Wrote: [ -> ]Ofnuts, I tryed it, but still the time out error came up.
Thank you

Code?