好友
阅读权限 10
听众
最后登录 1970-1-1
详细源码如下,但是运行起来1000条需要1到2分钟,好慢啊
package main
import (
"fmt"
"net/http"
"net/url"
"io"
"os"
"bufio"
"time"
"sync"
"strconv"
)
//线程控制变量-全局函数
var threads_go int //当前线程标示
var file_flags int //线程读取到的数据位置
var dirdata [] string //用于存储读取到的行
//存储扫描文件 map 类型
var file_data_A map [ int string
var file_data_B int
//动态数组结构体
type A struct {
numbs int
}
var dynaArr []A
//线程阻塞
var wg sync.WaitGroup
func main () {
currentTime :=time. Now ()
fmt. Println ( "当前时间" ,currentTime)
url := "http://www.baidu.com/ "
write_file ()
read_file_data ()
for k := 0 ; k < 10 ; k++ {
wg. Add ( 1 )
go flags_threads (url,k)
file_flags++
time. Sleep (time.Second)
//fmt.Println("当前k",k)
}
//time.Sleep(time.Second * 10)//暂时挂起主线程,让goroutine的两个线程跑完
fmt. Println ( "开始阻塞" )
wg. Wait ()
fmt. Println ( "任务执行结束,解除阻塞" )
fmt. Println (dynaArr)
currentTime2 :=time. Now ()
fmt. Println ( "结束时间" ,currentTime2)
}
//配合文件进行扫描g
func flags_threads (URL_file string ,flags int ) {
//fmt.Println("我是傻逼线程:",flags)
//思路:获取文件第一行URL,处理第一个,以此类推
//fmt.Printf("当数组:%d\r\n", file_lines[0])
fmt. Printf ( "当前处理任务线程file_flags:%d \r\n " , file_flags)
//fmt.Printf("当前处理任务+1:%d\r\n", file_flags+1)
//fmt.Printf("我处理:%d,和%d", dynaArr[file_flags-1].numbs,dynaArr[file_flags].numbs)
//创建文件流
//fmt.Printf("我从这里跳出来了:%d\r\n", len(file_data_A))
var for1 int =file_flags
var for2 int =(file_flags- 1 )
//fmt.Printf("当数1:%d\r\n", for1)
//fmt.Printf("当数2:%d\r\n", for2)
//f, _ := os.OpenFile("./test.txt", os.O_WRONLY|os.O_APPEND, 0666)
for i := dynaArr[for2].numbs; i < dynaArr[for1].numbs; i++ {
//截取文件内容拼接url
//fmt.Printf("当前处理:", i)
var sum_url string = URL_file + file_data_A
//fmt.Printf("当前处理URL:%d\r\n", sum_url)
u , _ := url. Parse (sum_url)
res , err := http. Get (u. String ())
if err != nil {
//fmt.Println("for1:",for1)
//fmt.Println("for2:",for2)
//fmt.Println("是我这里出错了:",u)
continue
//return
}
resCode := res.StatusCode
var tmp_sum string
resCode_string :=strconv. Itoa (resCode)
iiiiii :=strconv. Itoa (i)
tmp_sum =sum_url+ "——" +resCode_string+ "——" + "我是第" +iiiiii+ "个" + " \n "
if (resCode== 200 ) || (resCode== 403 )||(resCode== 500 ){
fmt. Printf ( "TypeCode:%d \n " , resCode)
fmt. Printf ( "当前处理URL:%s \r\n " , tmp_sum)
}
defer res.Body. Close ()
// defer res.Body.Close()
// if err != nil {
// fmt.Println("0")
// return
// }
//file_flags=file_flags+1
//fmt.Printf("TypeCode:%T\r\n", resCode)
//fmt.Printf("%d\n",i)
if i==( len (file_data_A)- 1 ){
//fmt.Printf("我从这里跳出来了:%d\r\n", len(file_data_A))
break
}
//var tmp_sum string
//resCode_string:=strconv.Itoa(resCode)
//iiiiii:=strconv.Itoa(i)
//tmp_sum=sum_url+"——"+resCode_string+"——"+"我是第"+iiiiii+"个"+"\n"
//fmt.Printf("当前处理URL:%s\r\n", tmp_sum)
//f.Write([]byte(string(tmp_sum)))
//time.Sleep(1 *time.Second)
//f.Close()
//fmt.Printf("完成第%d个:", i)
}
//fmt.Printf("TypeCode:%d\r\n", file_flags)
//fmt.Printf("%T", resCode)
//f.Close()
wg. Done ()
//return
}
//将扫描文件读取到全局变量file_data_A
func read_file_data () {
fi , err := os. Open ( "./urlscan.txt" )
if err != nil {
fmt. Printf ( "Error: %s \n " , err)
return
}
defer fi. Close ()
file_data_A = make ( map [ int string )
br := bufio. NewReader (fi)
for {
a , _ , c := br. ReadLine ()
if c == io.EOF {
break
}
file_data_A[ int (file_data_B)] = string (a)
file_data_B =file_data_B+ 1
//fmt.Println(string(a))
}
// for k, v := range test1 {
// fmt.Printf("key:%s value:%d \n", k, v)
// }
//fmt.Printf("key:%s", file_data_A[6954])
//fmt.Printf("key:%s", len(file_data_A))
var data_numb_cum int = len (file_data_A)/ 10
var data_numb_cum2 int = len (file_data_A)% 10
//fmt.Println("count :%d", data_numb_cum)
//fmt.Println("count2:%d", data_numb_cum2)
//把当前任务标示数添加到数组中
var test int
for i := 0 ; i <= 10 ; i++ {
if i < 10 {
t := A{test}
dynaArr = append (dynaArr, t)
} else {
t := A{test+data_numb_cum2}
dynaArr = append (dynaArr, t)
}
test =test+data_numb_cum
}
fmt. Println (dynaArr)
fmt. Println ( "chang arr" , len (file_data_A))
}
func write_file () {
filename := "./test.txt"
_ , err := os. Stat (filename)
if err == nil || os. IsExist (err) {
fmt. Println ( "the file already exists,remove." )
os. Remove (filename)
}
os. Create (filename)
}
运行图