django-registration and botscout
May 19, 2010
I have a website which users can register on. It's got the usual bot problems, and I decided to implement botscout. I have copied django-registration into my source, so as a quick solution I modified it to implement the call to botscout. I wouldn't normally recommend this approach, and I'm sure there's probably a better way to do it, but as a quick solution this did the job.
The code change was very simple. In django-registration's forms.py there's a RegistrationForm
. Add the following function to this form:
def clean_email(self): """ Query botscout to see if the email address is from a bot. """ lEmail = self.cleaned_data['email'] lConnection = httplib.HTTPConnection("botscout.com") lHeaders = {"Host" : "botscout.com", "User-Agent" : "Django", "Content-type": 'text/xml; charset="UTF-8"', "Content-length": "0", } lConnection.request("GET", "/test/?mail=%s" % lEmail, None, lHeaders) lResponseObject = lConnection.getresponse() lStatusCode = lResponseObject.status lResponse = lResponseObject.read() if lStatusCode == 200 and len(lResponse) > 0 and lResponse[0] == 'Y': raise forms.ValidationError("""Your email address appears on a bot register. If you are a real person, then please contact us via the feedback form and let us know.""") if len(lResponse) > 0 and lResponse[0] == '!': lErrorMessage = "Problems Accessing BotScout, %s:%s" % (lStatusCode, lResponse) send_mail('%s BotScout Error' % settings.EMAIL_SUBJECT_PREFIX, lErrorMessage, 'from@mail.com', ['to@mail.com'], fail_silently=True) return lEmail