要使路线生效,通常需要使用路由器或服务器配置。以下是一个使用Python的示例代码,可以通过设置网络接口的路由表来使路线生效。
import subprocess
def add_route(destination, gateway):
command = f"route add {destination} gateway {gateway}"
subprocess.run(command, shell=True)
def delete_route(destination):
command = f"route delete {destination}"
subprocess.run(command, shell=True)
# 设置路由
add_route("192.168.0.0/24", "192.168.1.1")
# 删除路由
delete_route("192.168.0.0/24")
在上述示例中,add_route
函数用于添加路由,delete_route
函数用于删除路由。使用subprocess.run
函数执行shell命令来配置路由表。
请注意,这只是一个示例代码,并且可能需要根据特定的操作系统和网络设置进行调整。