Python script closes after some iterations -


i wrote piece of code , when run goes smooth until gets "czz", beginner , not know problem.. if tell me doing wrong. idea behind code try find 3-letters domains available ".ro"

import urllib2 import urllib import string  urllib2 import request, urlopen, urlerror, httperror string import ascii_lowercase  f = open('3-letters.txt', 'w')  x in ascii_lowercase:     y in ascii_lowercase:         z in ascii_lowercase:             req = request("http://"+x+y+z+".ro")              try:                 response = urlopen(req)             except httperror e:                 f.write("http://"+x+y+z+".ro\n")             except urlerror e:                 f.write("http://"+x+y+z+".ro\n")             else:                 print "bad "+x+y+z   f.close(); 

one problem not closing connections after don't need them longer, can response.close(). in finally block ensure executed.

try:     # stuff except:     # stuff finally:     response.close() 

in addition, here's better way generate 3 letter strings nested for loops:

>>> string import ascii_lowercase >>> itertools import product >>> three_letters = (''.join(x) x in product(ascii_lowercase, repeat=3)) >>> x in three_letters: ...     print(x)  'aaa' 'aab' 'aac' 'aad' 'aae' ... 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -