external_application.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import re
  2. from urllib.parse import urlparse
  3. import requests
  4. from sqlalchemy import func
  5. from .engine import db
  6. from .types import StringUUID
  7. class ExternalApplication(db.Model):
  8. __tablename__ = "external_applications"
  9. __table_args__ = (db.PrimaryKeyConstraint("id", name="external_application_pkey"),)
  10. EXTERNAL_APPLICATION_TYPE_LIST = ["QUESTION_ANSWER", "SEARCH", "RECOMMEND"]
  11. id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
  12. name = db.Column(db.String(255), nullable=False)
  13. type = db.Column(db.String(255), nullable=False)
  14. url = db.Column(db.String(255), nullable=False)
  15. method = db.Column(db.String(255), nullable=False)
  16. # status = db.Column(db.Boolean, nullable=False, server_default=db.text("true"))
  17. created_by = db.Column(StringUUID, nullable=False)
  18. created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp())
  19. updated_by = db.Column(StringUUID, nullable=True)
  20. updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp())
  21. @property
  22. def status(self):
  23. # 0). 使用正则表达式验证URL格式
  24. regex = re.compile(
  25. r'^(?:http|ftp)s?://' # http:// or https://
  26. r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
  27. r'localhost|' # localhost...
  28. r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
  29. r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
  30. r'(?::\d+)?' # optional port
  31. r'(?:/?|[/?]\S+)$', re.IGNORECASE)
  32. is_valid_format = re.match(regex, self.url) is not None
  33. if not is_valid_format:
  34. return False
  35. # 1). 使用urllib库进行URL解析
  36. parsed_url = urlparse(self.url)
  37. # 2). 检查URL的scheme部分
  38. is_valid_scheme = parsed_url.scheme in ["http", "https"]
  39. if not is_valid_scheme:
  40. return False
  41. # 3). 检查URL的netloc部分
  42. is_valid_netloc = parsed_url.netloc
  43. if not is_valid_netloc:
  44. return False
  45. # 4). 使用requests库发送HTTP请求
  46. try:
  47. response = requests.get(self.url, timeout=1)
  48. if response.status_code == 200:
  49. return True
  50. else:
  51. return False
  52. except requests.exceptions.RequestException:
  53. return False