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
Post a Comment