Python 脚本转 Go 语言:证书下载与配置功能实现
Python 脚本转 Go 语言:证书下载与配置功能实现
本文将提供 Python 脚本转换为 Go 语言的示例,实现证书下载和配置文件写入功能。
Python 脚本:
import tkinter as tk
from tkinter import messagebox
import os
import configparser
import requests
import sys
# 下载证书
def download_file():
proxies = {
'http': 'http://127.0.0.1:8080',
'https': 'http://127.0.0.1:8080 ',
}
url = 'http://mitm.it/cert/p12'
# 下载到桌面
filename = os.path.join(os.path.expanduser('~'), 'Desktop', 'mitmproxy-ca-cert.p12')
# 发送请求并下载文件
response = requests.get(url=url, proxies=proxies)
with open(filename, 'wb') as f:
f.write(response.content)
# 写入配置文件
def set_ini(name):
path = os.path.join(os.environ['USERPROFILE'], 'HC.ini') # 获取文件路径
config = configparser.ConfigParser()
config.add_section('general')
with open(path, mode='w') as config_file:
config.write(config_file)
class App:
def button_click(self):
entry_text = self.entry.get() # 获取编辑框内容 else:
if entry_text == '':
messagebox.showerror('错误', '请输入编号')
download_file()
# cert = '321'
set_ini(entry_text)
self.entry.config(state='readonly')
messagebox.showinfo('信息', '设置完成,证书已下载到桌面请手动安装')
self.master.destroy()
def center_window(self):
'''将窗口居中'''
width = self.master.winfo_reqwidth()
height = self.master.winfo_reqheight() // 2
screen_width = self.master.winfo_screenwidth()
screen_height = self.master.winfo_screenheight()
x = int((screen_width - width) / 2)
y = int((screen_height - height) / 2)
self.master.geometry('{}x{}+{}+{}'.format(width, height, x, y))
a = App()
Go 语言实现:
package main
import (
"fmt"
"os"
"io/ioutil"
"net/http"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"gopkg.in/ini.v1"
)
func downloadFile() {
proxies := map[string]string{
"http": "http://127.0.0.1:8080",
"https": "http://127.0.0.1:8080",
}
url := "http://mitm.it/cert/p12"
filename := fmt.Sprintf("%s/Desktop/mitmproxy-ca-cert.p12", os.Getenv("USERPROFILE"))
resp, _ := http.Get(url)
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
ioutil.WriteFile(filename, content, 0644)
}
func setIni(name string) {
path := fmt.Sprintf("%s/HC.ini", os.Getenv("USERPROFILE"))
cfg, _ := ini.Load(path)
cfg.Section("general").Key("name").SetValue(name)
cfg.SaveTo(path)
}
func main() {
var entry *walk.LineEdit
mw := &walk.MainWindow{}
MainWindow{
AssignTo: &mw,
Title: "App",
Layout: VBox{},
Children: []Widget{
LineEdit{
AssignTo: &entry,
},
PushButton{
Text: "OK",
OnClicked: func() {
entryText := entry.Text()
if entryText == "" {
walk.MsgBox(mw, "Error", "请输入编号", walk.MsgBoxIconError)
downloadFile()
} else {
setIni(entryText)
entry.SetReadOnly(true)
walk.MsgBox(mw, "Info", "设置完成,证书已下载到桌面请手动安装", walk.MsgBoxIconInformation)
mw.Close()
}
},
},
},
}.Create()
mw.Run()
}
功能说明:
- 证书下载: 两个版本都使用
requests库 (Python) 或net/http包 (Go) 从指定 URL 下载证书文件。 - 配置文件写入: Python 使用
configparser模块,Go 使用ini包来操作配置文件。 - 用户界面: Python 使用
tkinter库,Go 使用walk库构建简单的用户界面,包含一个输入框和一个按钮。
注意事项:
- 确保已经安装了
requests,configparser(Python) 和walk,ini(Go) 等必要的库和包。 - 将
http://127.0.0.1:8080替换成实际的代理服务器地址。 - 修改
mitmproxy-ca-cert.p12文件名和路径,以符合实际需求。
本文展示了将 Python 脚本转换为 Go 语言的基本步骤。你可以根据实际情况修改代码,实现更多功能。
原文地址: https://www.cveoy.top/t/topic/nRJ9 著作权归作者所有。请勿转载和采集!