当前位置 博文首页 > 浅析Go语言版本的forgery

    浅析Go语言版本的forgery

    作者:xingyys 时间:2021-06-26 17:43

    使用过Python语言的朋友们可能使用过 forgery_py ,它是一个伪造数据的工具。能伪造一些常用的数据。在我们开发过程和效果展示是十分有用。但是没有Go语言版本的,所以就动手折腾吧。

    从源码入手

    在forgery_py的 PyPi 有一段的实例代码:

    >>> import forgery_py
    >>> forgery_py.address.street_address()
    u'4358 Shopko Junction'
    >>> forgery_py.basic.hex_color()
    '3F0A59'
    >>> forgery_py.currency.description()
    u'Slovenia Tolars'
    >>> forgery_py.date.date()
    datetime.date(2012, 7, 27)
    >>> forgery_py.internet.email_address()
    u'brian@zazio.mil'
    >>> forgery_py.lorem_ipsum.title()
    u'Pretium nam rhoncus ultrices!'
    >>> forgery_py.name.full_name()
    u'Mary Peters'
    >>> forgery_py.personal.language()
    u'Hungarian'

    从以上的方法调用我们可以看出forgery_py下有一系列的 *.py 文件,里面有各种方法,实现各种功能,我们在来通过分析下Python版本的forgery_py的源码来看看它的实现原理。

    # ForgeryPy 包的一级目录
    ├── dictionaries # 伪造内容和来源目录,目录下存放的都是一些文本文件
    ├── dictionaries_loader.py # 加载文件脚本
    ├── forgery    # 主目录,实现各种数据伪造功能,目录下存放的都是python文件
    ├── __init__.py

    我们在来看下forgery目录下的脚本

    $ cat name.py
    import random
    from ..dictionaries_loader import get_dictionary
    __all__ = [
      'first_name', 'last_name', 'full_name', 'male_first_name',
      'female_first_name', 'company_name', 'job_title', 'job_title_suffix',
      'title', 'suffix', 'location', 'industry'
    ]
    def first_name():
      """Random male of female first name."""
      _dict = get_dictionary('male_first_names')
      _dict += get_dictionary('female_first_names')
      return random.choice(_dict).strip()

    __all__ 设置能被调用的方法。

    first_name() 方法是forgery_py中一个典型伪造数据方法,我们只要来分析它就可以知道forgery_py的工作原理了。

    这个方法代码很少,能容易就看出 _dict = get_dictionary('male_first_names') _dict += get_dictionary('female_first_names') 获取的数据合并,在最后的 return random.choice(_dict).strip() 返回随机的数据。它的重点在于 get_dictionary() ,所以我们需要来看它的所在位置 dictionaries_loader.py 文件。

    $ cat dictionaries_loader
    import random
    DICTIONARIES_PATH = abspath(join(dirname(__file__), 'dictionaries'))
    dictionaries_cache = {}
    def get_dictionary(dict_name):
      """
      Load a dictionary file ``dict_name`` (if it's not cached) and return its
      contents as an array of strings.
      """
      global dictionaries_cache
      if dict_name not in dictionaries_cache:
        try:
          dictionary_file = codecs.open(
            join(DICTIONARIES_PATH, dict_name), 'r', 'utf-8'
          )
        except IOError:
          None
        else:
          dictionaries_cache[dict_name] = dictionary_file.readlines()
          dictionary_file.close()
      return dictionaries_cache[dict_name]
    js
下一篇:没有了