...
open(FILE, 'directories.txt');
foreach $line () {
# 刪除\r\n
chop $line;
}
read more
總有新鮮事~
...
# 使用 IO::Socket 模組
use IO::Socket;
# 用法 postHTMLsock(URL, post_data, other_request)
# ex. postHTMLsock("http://www.google.com", "view=Help", "Cookie: this-is-cookie\r\n");
sub postHTMLsock
{
$url = $_[0];
$post_data = $_[1];
$other_req = $_[2];
if($url && $post_data) {
# 比對網頁位址是否合乎格式?
($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 "POST $file HTTP/1.1\r\n";
if($other_req) {
print $socket "$other_req";
}
print $socket "Host: $host\r\n\r\n";
print $socket $post_data;
$socket;
}
else {NULL;}
}
else {NULL;}
}
...
# 使用 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;}
}