電子書籍の厳選無料作品が豊富!

ので動作を見るためにとほほを見て
WidnowsMeで

x.pl:
use Socket;
$addr=(gethostbyname("www.ndl.go.jp"))[4];
$name=pack("S n a4 x8", 2, 80, $addr);
socket(S,PF_INET,SOCK_STREAM,0);
connect(S, $name);
binmode(S);
select(S);
$|=1;
select(stdout);
print S "GET index.html HTTP/1.0\r\n\r\n";
while(<S>){print;}
close(S);

を作り
perl x.pl
を実行すると

HTTP/1.1 400 Bad Request
Date: Fri, 04 Feb 2005 15:40:15 GMT
Server: Apache
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>

が帰ってきました
なお
http://www.ndl.go.jp/index.html
は国立国会図書館のページでブラウザのアドレス欄に入れると正常に見れます

どのように修正すればよいでしょうか?

A 回答 (2件)

index.htmlの前に/をつけます。


print S "GET /index.html HTTP/1.0\r\n\r\n";

GETの次の部分は、参考URLのRFCでいうところのabs_path
(絶対パス)になりますので、「/」が必要になります。

上の修正でとりあえず通りますが、
最近は以下のような書き方をする方が多いのでは
ないかと思います。

#!/usr/bin/perl

use IO::Socket;

my ($sock) = IO::Socket::INET->new(
PeerHost => "www.ndl.go.jp",
PeerPort => 80,
Proto => 'tcp');
eval {
$sock->print("GET /index.html HTTP/1.0\r\n");
$sock->print("\r\n");
print <$sock>;
};
$sock->close();
if($@) {
die $@;
}

参考URL:http://www.w3.org/Protocols/rfc2616/rfc2616.html
    • good
    • 0
この回答へのお礼

ありがとうございます
動きました

use Socket;
$server = 'www.ndl.go.jp' ;
$port = getservbyname('http','tcp');
$struct = sockaddr_in($port,inet_aton($server));
socket(S,PF_INET,SOCK_STREAM,0) || die("Socket失敗 $!");
connect(S, $struct ) || die("接続失敗1 $!") ;
select(S); $| = 1; select(STDOUT);
$respons = <S> ;
unless($respons =~ /^220/)
{
close(S);die("接続失敗2 $!") ;
}
close(S); select(STDOUT);
print "正常に接続されました。\n";

が接続失敗1を出すのですがその原因も教えていただければ幸いです

お礼日時:2005/02/05 01:39

#1の方が仰るとおり、絶対パスでしていします。


あと、HTTP/1.1の書き方では

print S "GET /index.html HTTP/1.1\n";
print S "Host:www.ndl.go.jp\n\n";

かな。
    • good
    • 0
この回答へのお礼

ありがとうございます
print S "GET /index.html HTTP/1.1\n";
print S "Host:www.ndl.go.jp\n\n";
でも動きました
これでいこうと思います

お礼日時:2005/02/05 01:40

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!