JAVA程序员学Python一周开发的命令行小工具

2,750 阅读3分钟

初学python, 开发一款程序员多功能小工具,取名x-tools,目前只包含数据转换处理、http请求工具、文件下载工具、密码生成工具,后续会不断完善,改进,欢迎大家一起交流学习。

安装尝鲜

!!!先确保你是mac系统或linux系统,并且已安装了 python3、pip3、wget 工具,拷贝以下命令执行。

curl -fsSL https://raw.githubusercontent.com/telzhou618/x-tools/main/install.sh | bash

然后执行 x-tools,有如下显示说明安装成功。

> x-tools

Usage: x-tools [OPTIONS] COMMAND [ARGS]...

  x-tools is a collection of tools developed in Python

Options:
  -h, --help  Show this message and exit

Commands:
  data      Data processor
  file      File download
  password  Password generate
  request   Http request tools

开胃菜 - 先来几个应用场景

10W+Execl 数据如何转成批量SQL提交给运维导入数据库?

10w+条数据,csv格式 ,需要转换成Inert SQL 语句,每1000条分隔为一条批量插入语句,可执行如下命令生成,最后将生成好语句文件提交给运维执行即可,默认表名和csv文件名相同。

x-tools data --csv-to-sql ./t_user.csv -d 1000  -o ./t_user.sql
  • -csv-to-sql CSV文件转SQL语句
  • -d 多条数据合为一条批量SQL语句
  • -o 输出到指定文件,默认为当前目录

如何生成一个安全可靠的密码?

生成一个长度为16为的密码

x-tools password -c 16
# 输出:pzem8JEKr^bckGvN

如何优雅的发起 http请求 ?

GET请求:

x-tools request https://www.httpbin.org/get
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "www.httpbin.org",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36",
    "X-Amzn-Trace-Id": "Root=1-61ee5456-001093d3593fd9f254deabaf"
  },
  "origin": "101.68.65.50",
  "url": "https://www.httpbin.org/get"

POST请求:

x-tools request https://www.httpbin.org/post -m post -j {\"p1\":\"v1\"}
{
  "args": {},
  "data": "{\"p1\": \"v1\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "12",
    "Content-Type": "application/json",
    "Host": "www.httpbin.org",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36",
    "X-Amzn-Trace-Id": "Root=1-61ee544f-5ab2eb2d2ea0359430d541fa"
  },
  "json": {
    "p1": "v1"
  },
  "origin": "101.68.78.162",
  "url": "https://www.httpbin.org/post"
}

如何高效下载文件(显示进度条) ?

x-tools file -url https://vscode.cdn.azure.cn/stable/e5a624b788d92b8d34d1392e4c4d9789406efe8f/VSCodeUserSetup-x64-1.51.1.exe

image.png

更多功能和帮助

# x-tools data 帮助文档
> x-tools data --help 

Usage: x-tools data [OPTIONS]

  Data processor

Options:
  -csv-to-sql, --csv-to-sql TEXT  		Convert csv to sql
  -csv-to-json, --csv-to-json TEXT    Convert csv to json
  -csv-to-jsonl, --csv-to-jsonlines TEXT Convert csv to jsonlines
  -csv-to-xls, --csv-to-xls TEXT  		Convert csv to xls
  -csv-to-xlsx, --csv-to-xlsx TEXT  	Convert csv to xlsx
  -d, --divide-limit INTEGER      		Limit number of batch sql generated
  -o, --out-file TEXT             		Output to file
  --help                          		Show this message and exit.
# x-tools password 帮助文档
> x-tools password --help 

Usage: x-tools password [OPTIONS]

  Password generate

Options:
  -c, --count INTEGER       Length of password, default is 16 chars
  -A, --upper-az BOOLEAN    Contain characters[A~Z]
  -a, --letter-az BOOLEAN   Contain characters[a~z]
  -n, --number BOOLEAN      Contain characters[0~9]
  -s, --special BOOLEAN     Contain characters[!@#$%^&*]
  -all, --all-char BOOLEAN  Contain all characters
  -o, --out-file TEXT       Output to file
  -his, --history BOOLEAN   History generated password
  --help                    Show this message and exit.
# x-tools request 帮助文档
> x-tools request --help 

Usage: x-tools request [OPTIONS] URL

  Http request tools

  Example:

      x-tools request https://www.httpbin.org/get
      x-tools request https://www.httpbin.org/post -m post -j {\"p1\":\"v1\"}

Options:
  -m, --method [get|post|head|put|delete|options|patch]
                                  Request method
  -h, --headers TEXT              Headers dict
  -p, --params TEXT               Params dict
  -j, --json-params TEXT          Json data dict
  -f, --files TEXT                Upload files
  -fr, --format-result [text|json]
                                  Format return data
  --help                          Show this message and exit.
# x-tools file 帮助文档
> x-tools file --help 

Usage: x-tools file [OPTIONS]

  File download

Options:
  -url, --url TEXT    file url  [required]
  -name, --name TEXT  Picture rename
  --help              Show this message and exit.

项目地址

github.com/telzhou618/…

欢迎star,欢迎初学python且希望能发布一款自己的python程序的小伙伴一起交流学习。