2016-09-20 58 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:'的縮進也需要修復 –