Skip to main content
 首页 » 编程设计

python之如何在 ssh session 中响铃

2024年10月25日16unruledboy

我想用 Python 生成铃声或任何类型的音频。特别是,我假设我有一个打开的 ssh session 到另一台计算机,但我希望在本地听到音频。

这可能吗?我试过了

print '\a'   

即使我的声卡正常工作,它也不起作用,甚至在本地也不行。

请您参考如下方法:

这里有两个不同的问题:

  1. 使用 Python 发出声音。

这取决于您运行代码的系统。

这是取自 answer 的示例:

#windows 
import winsound 
winsound.Beep(300,2000) 
#where a is the frequency and b is the duration in miliseconds 
 
#linux 
 
import os 
os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % ( a, b)) 
  1. 通过 SSH 远程播放声音。

如果你想在本地播放远程声音,你需要转发声音,很像 X 转发,see this detailed SuperUser answer for how to do that.您需要一个名为 paprefs 的单独应用程序来执行此操作。

如果您只想远程发出噪音,那么当您执行脚本时它应该可以正常工作,具体取决于哪个 shell session 控制了音频(如果另一个用户登录并使用扬声器播放音乐怎么办? ) 这是一个相关的 Unix/Linux question