70 lines
1.8 KiB
Python
Executable File
70 lines
1.8 KiB
Python
Executable File
from fastmcp import FastMCP
|
||
from spider.mail_qq import start as a1
|
||
import subprocess
|
||
import asyncio
|
||
import tempfile
|
||
import os
|
||
import logging
|
||
|
||
mcp = FastMCP("spider-server")
|
||
logging.basicConfig(level=logging.INFO)
|
||
# 日志会输出到 stderr,不会污染 stdout
|
||
logging.info("服务器启动")
|
||
@mcp.tool()
|
||
def mail_qq_spider(account: str) -> str:
|
||
"""qq邮箱爬虫,第一个参数是用户名"""
|
||
return a1(account)
|
||
|
||
# 添加一个简单的工具
|
||
@mcp.tool()
|
||
def hello(name: str) -> str:
|
||
"""Say hello to someone"""
|
||
return f"Hello, {name}!"
|
||
|
||
|
||
|
||
@mcp.tool()
|
||
async def run_spider_in_vnc(index: int) -> str:
|
||
"""在 VNC 会话中运行爬虫
|
||
|
||
Args:
|
||
index: VNC 显示编号
|
||
"""
|
||
try:
|
||
# 调用外部 start.sh 脚本,传入 index 作为参数
|
||
process = await asyncio.create_subprocess_exec(
|
||
"/home/dgs/vnc-server/start.sh",
|
||
str(index),
|
||
stdout=asyncio.subprocess.PIPE,
|
||
stderr=asyncio.subprocess.PIPE
|
||
)
|
||
stdout, stderr = await process.communicate()
|
||
if process.returncode == 0:
|
||
return f"✅ 爬虫执行成功\n输出:\n{stdout.decode()}"
|
||
else:
|
||
return f"❌ 爬虫执行失败\n错误:\n{stderr.decode()}"
|
||
except Exception as e:
|
||
return f"❌ 执行出错: {str(e)}"
|
||
|
||
@mcp.tool()
|
||
async def stop_vnc_server(index: int, username: str = None) -> str:
|
||
"""停止 VNC 服务器"""
|
||
|
||
if username is None:
|
||
username = f"user{index}"
|
||
|
||
result = subprocess.run(
|
||
f'sudo su - {username} -c "vncserver -kill :{index}"',
|
||
shell=True,
|
||
capture_output=True,
|
||
text=True
|
||
)
|
||
|
||
return f"VNC 服务器已停止: {result.stdout}"
|
||
|
||
|
||
# 运行服务器
|
||
if __name__ == "__main__":
|
||
mcp.run(transport="sse")
|
||
# mcp.run(transport="stdio")
|
||
|