I am trying to figure out how to use the try and except to handle a socket timeout.
from socket import *
def main():
client_socket = socket(AF_INET,SOCK_DGRAM)
client_socket.settimeout(1)
server_host = 'localhost'
server_port = 1234
while(True):
client_socket.sendto('Message',(server_host,server_port))
try:
reply, server_address_info = client_socket.recvfrom(1024)
print reply
except socket.Timeouterror:
#more code
The way I added the socket module was to import everything, but how do I handle exceptions in the docs it says you can use socket.timeouterror, but that doesn't work for me. Also, how would I write the try exception block if I did import socket? Can someone also explain the difference in the imports.
解决方案from foo import *