Gopher クライアントを作りたい(3)
RFPを読んでみて、とりあえず
- 基本作法 =
『70番ポートで接続』
『<CR><LF>送信すると、接続先サーバの持っている情報リストが返ってくる』
『クライアントからの様々なコマンド送信によって結果が返ってくる』
『サーバーはドキュメントの最後の行でドット(.) を送信し、コネクションを切断する』
あたりを実装すれば、Gopher の原型ができそうだと思いました。
試しに、telnet を Gopher クライアント替わりにして Gopher サーバーにアクセスしてみようと思います。
今でも生きている Gopher サーバー(これを探すのに手こずりました。。検索して見つけました)
gopher.quux.org
に、70番ポートで telnet 接続してみると、当然つながりました。
$ telnet gopher.quux.org 70 Trying 78.47.53.23... Connected to www.quux.org. Escape character is '^]'.
その後、リターンキーを押すと。。。
iWelcome to gopher at quux.org! fake (NULL) 0 i fake (NULL) 0 iThis server has a lot of information of historic interest, fake (NULL) 0 ifunny, or just plain entertaining -- all presented in Gopher. fake (NULL) 0 iThere are many mirrors here of rare or valuable files with the fake (NULL) 0 iaim to preserve them in case their host disappears. PLEASE READ fake (NULL) 0 i"About This Server" FOR IMPORTANT NOTES AND LEGAL INFORMATION. fake (NULL) 0 i fake (NULL) 0 0About This Server /About This Server.txt gopher.quux.org 70 + 1Archives /Archives gopher.quux.org 70 + 1Books /Books gopher.quux.org 70 + 1Communication /Communication gopher.quux.org 70 + iThis directory contains the entire text of the book fake (NULL) 0 i"We the Media: Grassroots Journalism by the People, for the People" fake (NULL) 0 iby Dan Gillmor in various formats. fake (NULL) 0 i fake (NULL) 0 iFeel free to download and enjoy. fake (NULL) 0 1Computers /Computers gopher.quux.org 70 + 1Current Issues and Events (Updated Apr. 23, 2002) /Current gopher.quux.org 70 + 1Development Projects /devel gopher.quux.org 70 + 0Gopher's 10th Anniversary /3.0.0.txt gopher.quux.org 70 1Government, Politics, Law, and Conflict /Government gopher.quux.org 70 + 0How To Help /How To Help.txt gopher.quux.org 70 + 1Humor and Fun /Humor and Fun gopher.quux.org 70 + 1Index to Quux.Org /Archives/index gopher.quux.org 70 1Internet /Internet gopher.quux.org 70 + 1Other Gopher Servers /Software/Gopher/servers gopher.quux.org 70 1People /People gopher.quux.org 70 + 1Reference /Reference gopher.quux.org 70 + 1Software and Downloads /Software gopher.quux.org 70 + 1The Gopher Project /Software/Gopher gopher.quux.org 70 0What's New /whatsnew.txt gopher.quux.org 70 + Connection closed by foreign host. $
おぉ!リストが得られました!!
最後に自動でコネクションが切られ、プロンプト『$』表示に戻っているのも確認できました。
このサーバが動いていることが確認できたので、同様の動きをするプログラムを Python で書いてみることにしました。
from socket import * s = socket(AF_INET, SOCK_STREAM) s.connect( ('gopher.quux.org', 70) ) s.send('\r\n') msg = '' while True: chunk = s.recv(8192) if not chunk: break msg += chunk #s.close() #サーバがコネクションを切るので不要だった print msg
として実行してみると
iWelcome to gopher at quux.org! fake (NULL) 0 i fake (NULL) 0 iThis server has a lot of information of historic interest, fake (NULL) 0 ifunny, or just plain entertaining -- all presented in Gopher. fake (NULL) 0 iThere are many mirrors here of rare or valuable files with the fake (NULL) 0 iaim to preserve them in case their host disappears. PLEASE READ fake (NULL) 0 i"About This Server" FOR IMPORTANT NOTES AND LEGAL INFORMATION. fake (NULL) 0 i fake (NULL) 0 0About This Server /About This Server.txt gopher.quux.org 70 + 1Archives /Archives gopher.quux.org 70 + 1Books /Books gopher.quux.org 70 + 1Communication /Communication gopher.quux.org 70 + iThis directory contains the entire text of the book fake (NULL) 0 i"We the Media: Grassroots Journalism by the People, for the People" fake (NULL) 0 iby Dan Gillmor in various formats. fake (NULL) 0 i fake (NULL) 0 iFeel free to download and enjoy. fake (NULL) 0 1Computers /Computers gopher.quux.org 70 + 1Current Issues and Events (Updated Apr. 23, 2002) /Current gopher.quux.org 70 + 1Development Projects /devel gopher.quux.org 70 + 0Gopher's 10th Anniversary /3.0.0.txt gopher.quux.org 70 1Government, Politics, Law, and Conflict /Government gopher.quux.org 70 + 0How To Help /How To Help.txt gopher.quux.org 70 + 1Humor and Fun /Humor and Fun gopher.quux.org 70 + 1Index to Quux.Org /Archives/index gopher.quux.org 70 1Internet /Internet gopher.quux.org 70 + 1Other Gopher Servers /Software/Gopher/servers gopher.quux.org 70 1People /People gopher.quux.org 70 + 1Reference /Reference gopher.quux.org 70 + 1Software and Downloads /Software gopher.quux.org 70 + 1The Gopher Project /Software/Gopher gopher.quux.org 70 0What's New /whatsnew.txt gopher.quux.org 70 +
なんともあっけない。。
一応 socket を使ってみたのですが、Python には telnet モジュールもあったので使ってみると。。
from telnetlib import * tlnt = Telnet('gopher.quux.org', 70) tlnt.write('\r\n') print tlnt.read_all()
とすることで同じ結果を得られました。
Gopher では、いきなり <CR><LF> を送信せずに、検索クエリを入力すると検索結果のリストが得られますし、ドキュメントへのパスを入力するとそのドキュメントの内容が得られます。
なので、あとはサーバーへの接続後に送信する内容を変更することでいろんな操作ができる Gopher クライアントにできそうです。
そこまで本格的なクライアントを作りたい欲求が今はないので、「Gopher クライアントを作りたい」はここで終了。