Reputation: 11
i've struggled with this code and I can't see why its saying there is a syntax error on line 15. Im trying to create a TCP connection with google for a diagnostic tool for my college coursework.
import socket
import sys
#defining the port and host we wll be using to check the internet connection on.
remote_connection = "www.google.com" #google will automatically load balance the request to the uk servers.
port = 80
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
print("The script has exited with error %s" %(err))
try:
sockettest = socket.gethostbyname(remote_connection)
if socket.gaierror:
# this means that the connection is possibly blocked by a firewall and we will tell the end user.
print("there was an error resolving the host, possibly a firewall issue")
sys.exit()
elif socket.timeout:
#the connection has timedout which probably means the internet is offline
print("Your internet connection is offline, please connect all cables, reboot any network equiptment and try again")
sys.exit()
# connecting to the server
s.connect((remote_connection,port))
print("Your internet connection is active and working correctly! \n\
I was able to connect to:", (remote_connection), "sucessfully")
Upvotes: 0
Views: 1168
Reputation: 23504
There is no except
statement for
try:
sockettest = socket.gethostbyname(remote_connection)
Upvotes: 5