python - Closing connectionSocket throwing 'Invalid syntax with except IOError' -


i tried executing code, getting error message saying "invalid syntax". please me out, since new this.

#import socket module socket import *  connectonsocket.close() except ioerror:              connectionsocket.send('\nhttp/1.1 404 not found\n\n') #close client socket connectionsocket.close() serversocket.close() 

please see informative tutorialspoint page great explanation of try/except/else/finally python code!

your code should be:

#import socket module socket import *  try:     connectonsocket.open() except ioerror:                   connectionsocket.send('\nhttp/1.1 404 not found\n\n') #close client socket finally:     connectionsocket.close()     serversocket.close() 

and if copying , pasting code cmd/terminal cannot have blank lines (if you'll indentation error).

try/except/finally

try: means 'try' something, ie. call function, method, etc.

except (you can have multiple excepts): means if 'try' code did not work, 'this' in response.

finally: means 'this' stuff whether or not 'tried' , succeeded, or 'excepted'.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -