LoginSignup
8
5

More than 3 years have passed since last update.

Pythonで伺かのSSTPを使って通信する方法

Last updated at Posted at 2018-11-24

SSTPとは

Sakura Script Transfer Protocolの略称で、伺かにおける独自の通信プロトコル。実はIANAにも記載されている国際規格。(Secure Socket Tunneling Protocolではない。というかこっちの方が先だったはず)
ベースウェア(SSP)はこのプロトコルのポートが解放されたサーバーとしての機能を持つ。

SSTPに関する資料(特に具体的なコード)が少なくて苦労したので備忘録として置いておく

サンプル

# -*- coding:utf-8 -*-
import socket

host = "127.0.0.1"  #localhost
port = 9801  #The port number of SSTP

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client.connect((host, port))

test = "NOTIFY SSTP/1.5\r\n\
Sender: リサ\r\n\
Event: OnRelease\r\n\
Script: \\0\s[1]This text is sent from Python.\\nCan you see this?\\1This is a test\e\r\n\
Option: nodescript, notranslate\r\n\
Charset: UTF-8\r\n\
\r\n"

client.send(test.encode())
#send() receives in bytes representation

response = client.recv(4096)
#recv() determines the length(bytes) of response
client.close()
print(response.decode())
#The return is also byte-type so decode it

Senderは何でもよい(普通プログラム名にしておく)。Pythonでは\0,\1,\nはエスケープして\\0,\\1,\\nとしないと正常に動かない。
OptionのnodesrciptはSSTPマーカーを非表示、notranslateはSHIORI EVENTのOnTranslateに処理を渡さない用のオプション。必要に応じて付ける。
またNOTIFY以外にもEXECUTEなど別の処理も存在する。http://www.ooyashima.net/db/sstp.html を参照。

この処理はIFGhostを使って次のように書くこともできる:

"NOTIFY SSTP/1.5\r\n\
Sender: test\r\n\
Event: OnRelease\r\n\
IfGhost: リサ\r\n\
Script: \\0\s[1]Lisa is speaking now.\r\n\
IfGhost: エミリ, さくら\r\n\
Script: \\0\s[1]Sakura or Emily is speaking now.\r\n\
Option: nodescript, notranslate\r\n\
Charset: UTF-8\r\n\
\r\n"

IfGhostでヒットしなかった場合は一番上に書いた分岐が採用される(今の場合IfGhost:リサの分岐)

動作例

キャプチャ.PNG

何に使えるか

  • 今までSAORIを使って外部プログラムを実行していた時、YAYA及び里々ではマルチスレッドに対応していなかったのでSAORIの処理が終わるまでゴースト側は何も反応できず、処理に時間がかかるものを実行しているとフリーズしているように見えてしまう問題があった。 そこでSAORIの処理そのものをマルチスレッドで書き、別のスレッドで実行した結果をSSTPで渡すことでこの問題を解決できる。(実際音楽を再生するSAORIであるmciaudior.dllはこの方法を使っている……はず)
8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5