2014-02-13 123 views
-1

試圖做一個簡單的錯誤處理,看看主機是否無法訪問,我一直得到一個無效的語法except。錯誤處理python

import socket    

s = socket.socket()   
host = "" 
port = 1337    

print(""" 
============================================ 
Welcome to Coder77's local internet messager 
============================================ 
The current soon to be encrypted server is {0} 
""".format(host)) 

host = input("Please select the IP you would like to communicate to: ") 
print("Now connecting to {0}....".format(host)) 

try: 
     s.connect((host, port) 
except socket.error as s: 
     print ("Host is unreachable") 


print (s.recv(2048)) 
input("Close") 
s.close() 
+2

規則:每當你得到一個奇怪的語法錯誤,看看在*之前*網上看到如果括號,括號和括號是平衡的。 –

回答

3
s.connect((host, port) 

應該是拇指

s.connect((host, port)) 
3

變化s.connect((host, port)s.connect(host, port)

+1

不,它應該是's.connect((主機,端口))'。 ['socket.connect()'](http://docs.python.org/2/library/socket.html#socket.socket.connect)只需*一個*參數。 –