2016-09-20 32 views
0

我一直在努力处理这段代码,我看不出为什么它说第15行有语法错误。我正在尝试与Google建立一个TCP连接,以用于我的大学课程作业的诊断工具。使用套接字时语法无效

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") 
+0

什么是你所得到的确切的错误信息和你有什么话想纠正呢? – rjp

+0

@rjp无效的语法 – Interpolation

回答

5

没有except语句

try: 
    sockettest = socket.gethostbyname(remote_connection) 
+0

'if socket.gaierror:'的缩进也需要修复 –