# determine the type of file over networkres = urllib.request.urlopen(url)# get file content type (jpeg, pdf etc)file_type = res.info().get_content_subtype()# filename ofcfile_name = res.info().get_filename()# assuming file_name contains file extension as well# otherwise use file_type and name togetherurllib.request.urlretrieve(url, file_name)