【问题标题】:web2py url validatorweb2py url 验证器
【发布时间】:2012-08-11 20:53:10
【问题描述】:

在 web2by 构建的缩短器中,我想首先验证 url,如果它无效返回到第一页并显示错误消息。这是我在控制器(mvc 架构)中的代码,但我不明白出了什么问题..!!

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()

【问题讨论】:

  • 这是完整的代码吗?我看不到任何输入 URL(即表单)的方法。此外,您需要导入随机数和字符串。如果你能描述你的期望、你得到什么,并显示任何其他相关代码(例如,一个视图),这将有所帮助。
  • 用正则表达式检查网址不是更好吗,看看这个:stackoverflow.com/a/11967815/1248554
  • 不需要正则表达式,因为 web2py 已经包含一个 IS_URL 验证器。不过,我假设 OP 想要确认 URL 指向一个实时站点,而不仅仅是它是一个格式正确的 URL。
  • 谢谢 4,当我使用 urllib.urlopen 时,它会打开到网站的连接,就像 httplib.HTTPConnect 一样,但问题是当用户输入类似 jfvbhsjdfvbs.com的内容时会发生什么>
  • 问题是我应该如何处理非响应式网页..httplib..urllib.. 或其他一些。否则..

标签: python web2py urllib


【解决方案1】:

您不能使用 httplib 检查 http 响应代码吗?如果是 200 则页面有效,如果是其他任何内容(如 404)或错误则无效。

看到这个问题:What’s the best way to get an HTTP response code from a URL?

更新:

根据您的评论,您的问题似乎是您如何处理错误。您只处理 IOError 问题。在您的情况下,您可以通过切换到以下方式单独处理所有错误:

except:
    return index()

您还可以通过覆盖 http_default_error 来构建自己的异常处理程序。请参阅How to catch 404 error in urllib.urlretrieve 了解更多信息。

或者您可以切换到具有特定错误的 urllib2,然后您可以像这样处理 urllib2 抛出的特定错误:

from urllib2 import Request, urlopen, URLError
req = Request('http://jfvbhsjdfvbs.com')
try:
    response = urlopen(req)
except URLError, e:
    if hasattr(e, 'reason'):
        print 'We failed to reach a server.'
        print 'Reason: ', e.reason
    elif hasattr(e, 'code'):
        print 'The server couldn\'t fulfill the request.'
        print 'Error code: ', e.code
else:
    print 'URL is good!'

上面的代码将返回:

We failed to reach a server.
Reason:  [Errno 61] Connection refused

每个异常类的细节都包含在 urllib.error api 文档中。

我不确定如何将其插入您的代码中,因为我不确定您到底要做什么,但 IOError 不会处理 urllib 抛出的异常。

【讨论】:

  • def md5(): url = request.vars.url conn = httplib.HTTPConnection(url) try: conn.request("HEAD", "/") return dict(rand_url = ''. join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(6)), input_url=url) 除了 StandardError: print "invalid URL" 。 . . . gaierror: (11004, 'getaddrinfo 失败')
  • 谢谢,但是问题是web2py完全兼容python 2.5,听说python 2.5只有urllib,请问python 2.5可以用urllib2吗?
  • 是的,urllib2 应该在 python 2.5 中。这是来自 python 文档站点的api reference for python 2.5。
  • def random(): url = request.vars.url req = Request(url) try: response = urlopen(req) 除了 URLError, e: if hasattr(e, 'reason'): print “我们未能到达服务器。” print 'Reason: ', e.reason elif hasattr(e, 'code'): print '服务器无法完成请求。' print 'Error code: ', e.code else: return dict(rand_url = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(6)), input_url=url )
  • 真的很抱歉耽误您的时间,我很失望.. 我只是想检查一下我想缩短它的 url 的页面是否可用且有效.. 有时会写代码降低我们的速度和创造性思维。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多