发送数据的简单代码:
要使代码可以使用php直接执行,程序里使用的路径是绝对路径。
主要是将文件放到指定的目录下,读取文件中的匹配行,执行需要的请求操作。
<?php
ini_set('memory_limit','2048M');
set_time_limit(0);
function curlRequest($gets,$host="rubbish.com", $Post = 80) {
$handle = fsockopen($host, $Post, $errorCode, $errorMsg, 60);
$header = "GET $gets HTTP/1.1\r\n";
$header .= "Host: $host\r\n";
$header .= "Connection: keep-alive\r\n\r\n";
fwrite($handle, $header);
//这里可以参考request2来解析请求会的数据,包括请求头,但在发请求的过程中,该操作相当费劲。
fclose($handle);
return $errorCode;
}
//get all files ,and save to array
$starTime = microtime(true);
//使用这样,只是为了让php可以在控制台执行,下面是读取文件的操作,is_file参数是一个绝对路径。
$path = dirname(__FILE__);
$dir = "$path/log3";
$fileSet = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == '.' || $file == '..')
continue;
$filePath = "$dir/".$file;
if(is_file($filePath)){
$fileSet[] = $filePath;
}
}
closedir($dh);
}
}
$header = "/rubbish.html?";
$request = $header;
$i = 0;
$m = 0;
foreach($fileSet as $value){
//set pattern to filter
$regex = "/.*pattern\.(aspx|php|html)\?(.*) HTTP\/1\.0.*/i";
$handle = fopen($value,'r');
if($handle){
while(!feof($handle)){
$line = fgets($handle);
if(preg_match($regex,$line,$match)){
//rebuild request
$request = $request.$match[2];
// echo $request;
++$m;
echo ".";
$ret = curlRequest($request);
// $ret = true;
if($ret){
++$i;
}
usleep(200);
$request = $header;
}
}
}
fclose($handle);
}
$endTime = microtime(true);
echo "total : $m, fail request $i, waste time:".($endTime-$starTime);
exit;
在控制台,大概30分钟左右,平均发大概6万请求,读文件平均1.2G,耗时30分钟左