About Me

我的相片
Taipei<->HsinChu, Taiwan
我是 Mashi,叫我 媽許、罵許,我都會回頭XD
2007年12月3日 星期一

[Perl] getHTMLsock


# 使用 IO::Socket 模組
use IO::Socket;

# 用法 getHTMLsock(URL, other_request)
# ex. getHTMLsock("http://www.google.com", "Cookie: this-is-cookie\r\n");
sub getHTMLsock
{
$url = $_[0];
$other_req = $_[1];

if($url) {

# 比對網頁位址是否合乎格式?
($host, $file) = $url =~ m!http://([^/]+)(/[^\#]*)!;

# 若比對正確,才抓取
if ($host) {

# 產生一個 IO::Socket::INET 物件
$socket = IO::Socket::INET->new(
PeerAddr => $host, # 指定主機位址
PeerPort => 'http(80)' # 指定 port 號
);

# 針對 $socket 寫入,此動作形同對 $host 主機提出網頁檔 $file 的要求
print $socket "GET $file HTTP/1.1\r\n";
if($other_req) {
print $socket "$other_req";
}
print $socket "Host: $host\r\n\r\n";
$socket;
}
else {NULL;}
}
else {NULL;}
}

0 意見:

 
Blogger Template Layout Design by [ METAMUSE ] : Code Name BlackCat 2.0.0