import httplib
import re
md5 = raw_input('Enter MD5: ')
conn = httplib.HTTPConnection("www.md5.rednoize.com")
conn.request("GET", "?q="+ md5)
try:
response = conn.getresponse()
da开发者_开发技巧ta = response.read()
result = re.findall('<div id="result" >(.+?)</div', data)
print result
except:
print "couldnt find the hash"
raw_input()
I know I'm probably implementing the code wrong, but which exception should I use for this? if it cant find the hash then raise an exception and print "couldnt find the hash"
Since re.findall doesn't raise exceptions, that's probably not how you want to check for results. Instead, you could write something like
result = re.findall('<div id="result" >(.+?)</div', data)
if result:
print result
else:
print 'Could not find the hash'
If you realy like to have an exception there you have to define it:
class MyError(Exception):
def init(self, value):
self.value = value
def str(self):
return repr(self.value)
try:
response = conn.getresponse()
data = response.read()
result = re.findall('(.+?)</div', data)
if not result:
raise MyError("Could not find the hash")
except MyError:
raise
精彩评论