英文:
What causes this golang os.Exec behavior (escaped double quotes)?
问题
我有以下代码:
for i := 0; i < 1; i++ {
wait := rand.Int63n(150) + 300
time.Sleep(time.Millisecond * time.Duration(wait))
//
useragent := "--user-agent='Mozilla/5.0 (X11; CrOS armv7l 8872.76.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.105 Safari/537.36'"
outDirectory := "--output-document=temp/review-" + strconv.Itoa(i) + ".html"
header := "--header=\"Content-Type: application/x-www-form-urlencoded;charset=utf-8\""
uri := "https://play.google.com/store/getreviews"
payload := "--post-data=id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=" + strconv.Itoa(i)
cmd := exec.Command("wget", useragent, outDirectory, header, uri, payload)
fmt.Println(strings.Join(cmd.Args, " "))
bytes, _ := cmd.CombinedOutput()
fmt.Println(string(bytes))
}
fmt.Println("Finished crawling")
如果我将 payload 行从
payload := "--post-data=id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=" + strconv.Itoa(i)
改为
payload := "--post-data=\"id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=" + strconv.Itoa(i) + "\""
它会返回 Server Error 500,即使在运行相应的 wget
命令时:
wget --user-agent='Mozilla/5.0 (X11; CrOS armv7l 8872.76.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.105 Safari/537.36' --output-document=temp/review-0.html --header="Content-Type: application/x-www-form-urlencoded;charset=utf-8" https://play.google.com/store/getreviews --post-data="id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=0"
它将与未转义引号的 post-data 产生相同的响应。
为什么 Golang 的 os.Exec 会导致这种行为?
英文:
I have the following code:
for i := 0; i < 1; i++ {
wait := rand.Int63n(150) + 300
time.Sleep(time.Millisecond * time.Duration(wait))
//
useragent := "--user-agent='Mozilla/5.0 (X11; CrOS armv7l 8872.76.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.105 Safari/537.36'"
outDirectory := "--output-document=temp/review-" + strconv.Itoa(i) + ".html"
header := "--header=\"Content-Type: application/x-www-form-urlencoded;charset=utf-8\""
uri := "https://play.google.com/store/getreviews"
payload := "--post-data=id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=" + strconv.Itoa(i)
cmd := exec.Command("wget", useragent, outDirectory, header, uri, payload)
fmt.Println(strings.Join(cmd.Args, " "))
bytes, _ := cmd.CombinedOutput()
fmt.Println(string(bytes))
}
fmt.Println("Finished crawling")
If I change the payload line from
payload := "--post-data=id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=" + strconv.Itoa(i)
to
payload := "--post-data=\"id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=" + strconv.Itoa(i) + "\""
It will return Server Error 500, even though when running the corresponding wget
:
wget --user-agent='Mozilla/5.0 (X11; CrOS armv7l 8872.76.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.105 Safari/537.36' --output-document=temp/review-0.html --header="Content-Type: application/x-www-form-urlencoded;charset=utf-8" https://play.google.com/store/getreviews --post-data="id=fi.danskebank.mobilepay&reviewSortOrder=2&xhr=1&reviewType=0&pageNum=0"
It will have same response as the post-data without escaped quotes.
Why does Golang os.Exec result in this behavior?
答案1
得分: 3
当你在shell中执行命令时,shell会处理引号,但它们不会传递给你要执行的程序。当你在Go中执行命令时,不涉及shell处理,所以引号会被传递。由于程序不期望引号,它们会直接传递到请求体中,服务器会返回错误。
与你的问题无关,但如果你没有特定的原因使用wget,使用内置的HTTP客户端比每个请求都fork到wget要获得更好的性能。
英文:
When you execute the command in your shell, the shell processes the quotes and they aren't passed to the program you're executing. When you execute a command in Go, there is no shell processing involved, so the quotes are passed through. Since the program isn't expecting them, they get passed through directly in the request body, and the server responds with an error.
Unrelated to your issue, but if you don't have a specific reason to use wget, you'd get far better performance using the built-in HTTP client than forking out to wget for every request.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论