From eb4a5c07b33154b3c563fabe84501ed8b8037a34 Mon Sep 17 00:00:00 2001 From: John Chi Date: Tue, 12 Jun 2018 19:01:06 +0800 Subject: [PATCH] feat(validate input): complete transaction address and amount validation feature --- .gitignore | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ btmtransfer.py | 50 ++++++++++++++++++++++++++ httprequest.py | 22 ++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 .gitignore create mode 100755 btmtransfer.py create mode 100644 httprequest.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af6d81c --- /dev/null +++ b/.gitignore @@ -0,0 +1,110 @@ +# Created by .ignore support plugin (hsz.mobi) +### Python template +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +.DS_Store +.idea/ + diff --git a/btmtransfer.py b/btmtransfer.py new file mode 100755 index 0000000..05ded5d --- /dev/null +++ b/btmtransfer.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import os +import sys +import httprequest + + +def validate_address(line, address): + parameter = {'address': address} + data = httprequest.post('validate-address', parameter) + if not data['valid']: + print(line + 'This line transaction address is not validate') + sys.exit(1) + + +def validate_amount(line, amount): + try: + int(amount) + except ValueError: + print(line + 'This line transaction amount is not int.') + sys.exit(1) + + +def validate_input(argv): + if len(argv) < 2 or len(argv) >= 2 and not str(argv[1]).endswith('.txt'): + print('Input error:Use this script with input txt file.' + + '\n' + 'Example usage:' + '\n' + '\t./btmtransfer.py input.txt') + sys.exit(1) + + # relative path + path = os.path.abspath('.') + '/' + argv[1] + if not os.path.exists(path): + # absolute path + path = argv[1] + # read file + with open(path, 'r', encoding='utf-8') as file: + for line in file: + splits = line.split(',') + validate_address(line, splits[0]) + validate_amount(line, splits[1]) + + +def main(argv=None): + if argv is None: + argv = sys.argv + validate_input(argv) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/httprequest.py b/httprequest.py new file mode 100644 index 0000000..fa06c83 --- /dev/null +++ b/httprequest.py @@ -0,0 +1,22 @@ +import requests +import sys +import json + +host = 'http://localhost:9888' + + +def post(endpoint, parameter): + headers = {'Content-Type': 'application/json'} + data = json.dumps(parameter) + try: + request = requests.post(host + '/' + endpoint, headers=headers, data=data) + except Exception as e: + print(str(e) + '\n' + + 'Connection error:Make sure bytomd run at http://localhost:9888 before you run this script.') + sys.exit(1) + else: + result = json.loads(request.text) + if not result['status'] == 'success': + print('Server error:Bytomd can not provide' + endpoint + 'service.') + else: + return result['data'] -- 2.11.0