Also, note that requests is a third-party library, so you'll have to 
install it first. Also, keep in mind that when a URL is submitted for 
validation, there will be some delay, because your server will then have to 
make a request for the URL. If you've got lots of traffic, this could make 
it difficult to serve lots of simultaneous requests.

Anthony

On Wednesday, August 15, 2012 1:25:29 PM UTC-4, rochacbruno wrote:
>
> I would use something like this:
>
> *import requests*
>
> class IS_VERIFIED_URL(object):
>     def __init__(self, error_message="url does not exist!"):
>         self.error_message = error_message
>
>     def __call__(self, url):
>         error = None
>         *response = requests.get(url)*
>         if *response.status_code != 200*:
>             error = self.error_message 
>         return (url, error)
>
> So in FORM, MODEL or SQLFORM
>
> <db.table|INPUT>.requires = IS_VERIFIED_URL()
>
>
> http://rochacbruno.com.br/custom-validator-for-web2py-forms/
>
>
> *Bruno Cezar Rocha** - @rochacbruno*
> [email protected] <javascript:> | Mobile: +55 (11) 99210-8821
> www.CursoDePython.com.br | www.rochacbruno.com.br
> Blog: Open links which points outside your own site in a new 
> window<http://rochacbruno.com.br/open-links-which-points-outside-your-own-site-in-a-new-window/>
>   Get a signature like this. 
> <http://r1.wisestamp.com/r/landing?promo=18&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_18>
>  Click 
> here.<http://r1.wisestamp.com/r/landing?promo=18&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_18>
>    
>
>
>
> On Wed, Aug 15, 2012 at 12:01 PM, Hojat Taheri 
> <[email protected]<javascript:>
> > wrote:
>
>> in a shorten-er built by web2by i want to validate url's first, if it's 
>> not valid goes back to the first page with an error message. this is my 
>> code in controller (mvc arch.) but i don't get what's wrong..!!
>>
>> import urllib
>>
>> def index():
>> return dict()
>>
>> def random_maker():
>>
>>
>> url = request.vars.url
>> try:
>>     urllib.urlopen(url)
>>
>>
>>     return dict(rand_url = ''.join(random.choice(string.ascii_uppercase + 
>> string.digits + string.ascii_lowercase) for x in range(6)), input_url=url)
>>
>> except IOError:
>>     return index()
>>
>>   -- 
>>  
>>  
>>  
>>
>
>

-- 



Reply via email to