# NTP校时服务器
一个功能完整的NTP(Network Time Protocol)校时服务器,支持从多个公共NTP服务器获取最新时间,并为客户端提供校时服务。
## 功能特性
– 🕐 **多源时间同步**: 从多个公共NTP服务器获取时间,确保准确性
– 🔄 **自动同步**: 定期自动同步时间,保持时间精度
– 🌐 **客户端服务**: 为NTP客户端提供标准的校时服务
– 📊 **Web管理界面**: 现代化的Web界面,实时监控服务器状态
– 📈 **连接统计**: 统计客户端连接数量和活跃状态
– 🛠️ **手动控制**: 支持手动启动、停止和同步操作
– 📝 **详细日志**: 完整的操作日志记录
– 🚀 **生产就绪**: 支持生产环境部署
## 系统要求
– Python 3.7+
– Windows/Linux/macOS
## 安装步骤
1. **克隆或下载项目**
git clone https://github.com/liangkangtow/ntpserver.git
cd ntp校时服务器
2. **安装依赖**
pip install -r requirements.txt
3. **快速测试功能**
python quick_test.py
4. **启动Web管理界面**
python web_interface.py
5. **访问管理界面**
打开浏览器访问: http://localhost:5000
##使用方法
### 方法一:使用Web管理界面(推荐)
1. **开发环境启动**:
```bash
python web_interface.py
```
2. **生产环境启动**:
```bash
python start_production.py
```
3. 在浏览器中访问 http://localhost:5000
4. 点击"启动服务器"按钮启动NTP服务
5. 通过界面监控服务器状态和管理服务
### 方法二:直接运行NTP服务器
```bash
python ntp_server.py
```
### 方法三:使用启动脚本
**Windows用户**:
```bash
start.bat
```
**所有平台**:
```bash
python start_server.py
```
### 方法四:测试服务器功能
```bash
# 快速功能测试
python quick_test.py
# 测试本地NTP服务器
python ntp_client_test.py
# 测试指定服务器
python ntp_client_test.py 192.168.1.100
# 与公共NTP服务器对比测试
python ntp_client_test.py –compare
```
## 配置说明
### NTP服务器配置
在 `ntp_server.py` 中可以修改以下配置:
```python
# 服务器监听地址和端口
host = '0.0.0.0' # 监听所有网络接口
port = 123 # 标准NTP端口
# 时间同步间隔(秒)
sync_interval = 300 # 5分钟同步一次
# 公共NTP服务器列表
ntp_servers = [
'time.windows.com',
'time.nist.gov',
'pool.ntp.org',
'time.google.com',
'time.apple.com'
]
```
### Web界面配置
在 `web_interface.py` 中可以修改Web服务配置:
```python
# 开发环境配置
app.run(host='0.0.0.0', port=5000, debug=False)
# 生产环境配置(使用WSGI服务器)
serve(app, host='0.0.0.0', port=5000, threads=4)
```
### 生产环境部署
1. **使用WSGI服务器**:
```bash
python start_production.py
```
2. **设置环境变量**:
```bash
export FLASK_ENV=production
python web_interface.py
```
3. **使用系统服务**:
```bash
# 创建systemd服务文件
sudo nano /etc/systemd/system/ntp-server.service
```
**注意**: 在生产环境中使用前,请充分测试并确保符合相关安全要求。
评论前必须登录!
注册