gpt4 book ai didi

laravel - Curl 和 Laravel,总是重定向

转载 作者:行者123 更新时间:2023-12-02 13:34:27 27 4
gpt4 key购买 nike

我正在尝试对我的机器上运行的 Laravel (5.2) 应用程序进行简单的 curl :

curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1" http://192.168.1.65:8000/pt

结果如下:

请求 header :

*   Trying 192.168.1.65...
* Connected to 192.168.1.65 (192.168.1.65) port 8000 (#0)
> GET /pt HTTP/1.1
> Host: 192.168.1.65:8000
> Accept: */*
> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1

响应 header 和正文:

< HTTP/1.1 302 Found
< Host: 192.168.1.65:8000
< Connection: close
< X-Powered-By: PHP/7.0.8-0ubuntu0.16.04.2
< Set-Cookie: lang=pt; expires=Fri, 04-Nov-2016 08:53:50 GMT; Max-Age=2592000; path=/
< Cache-Control: no-cache
< Location: http://192.168.1.65:8000/pt
< Content-Type: text/html; charset=UTF-8
< Date: Wed, 05 Oct 2016 08:53:50 GMT
< Set-Cookie: XSRF-TOKEN=eyJpdiI6IjNReFJiRFpYOG5USEgzaVZ4YWQ5OXc9PSIsInZhbHVlIjoiblBFU0FqRjJ3WFMyajJHZnBlUEMzT2lXK2ZDaGpTVDJnQnZZSXdSNUhTUHQ2QmxjcUZGUDFOUit0NzFKeUxMY28zaUl0VlVBNGtUMUJmYnlxWisrT3c9PSIsIm1hYyI6IjZjZmFlZTcwNGMxOTE1OGM2NjE1ZWM5OWViZjEzMjZmYzIwZTljNWMwYWY1ZmQzZGI3Y2FjZDdiM2Q4Y2IxMmQifQ%3D%3D; expires=Wed, 05-Oct-2016 10:53:50 GMT; Max-Age=7200; path=/
< Set-Cookie: laravel_session=eyJpdiI6IjJ5MTMwYXBpVDlqRTZ6U2NmNjBWb3c9PSIsInZhbHVlIjoiTm10QklTZTAydURkeU1kSm9Eam1UaGg1RlpvQWpncTBJTmRSd2poT01ORVRUa2l3MzNSSjJZTStPMWpGTVdYQ0JFRkt3M2ZUd3NRYVNTS3JLQkpLckE9PSIsIm1hYyI6IjM5MmQ2YzEzNDYwM2M5YTc1MzI0ODZmMjBiYWZiNmYyM2Q4NzE0ZTEyOWE3NWUzZjRjMGIxMGFjMGVjZDgzNGIifQ%3D%3D; expires=Wed, 05-Oct-2016 10:53:50 GMT; Max-Age=7200; path=/; HttpOnly
<
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="1;url=http://192.168.1.65:8000/pt" />

<title>Redirecting to http://192.168.1.65:8000/pt</title>
</head>
<body>
Redirecting to <a href="http://192.168.1.65:8000/pt">http://192.168.1.65:8000/pt</a>.
</body>
* Closing connection 0

我发现这真的很奇怪,因为如果我使用来自 python 3.x 的 lib 请求:

import requests

headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1'}
req = requests.get('http://192.168.1.65:8000/pt', headers=headers)
print(req.text)

响应是页面中的所有 html(这就是应该发生的情况)

PS:我还对 curl 使用了 -L 标志,因此它可以闲置重定向,但它似乎最终陷入无限循环(curl: (47) 最多 (50) 个重定向)

为什么会出现这种情况?怎么解决?

最佳答案

您正在设置 cookie,如果该 cookie 不存在,则使用 302 Found 将用户重定向到同一页面,设置 cookie。具体来说,您正在检查 lang cookie。

虽然 Python 请求(显然)在重定向时保留 cookie,但默认情况下,curl 不会执行此操作。您可以通过添加 cookie jar(存储 cookie 的文件,-c cookies.txt)或手动设置 cookie (-b lang=pt) 来启用此功能。

例如:

$ curl -c cookies.txt -A 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' -LI lemos.migueldvl.com/pt
HTTP/1.1 301 Moved Permanently
Date: Wed, 05 Oct 2016 09:18:45 GMT
Server: Apache
X-Powered-By: PHP/5.6.26
Cache-Control: no-cache
Set-Cookie: XSRF-TOKEN=eyJpdiI6IjJtcHVwUlwvbzJSUlhublVzSCtPVk5BPT0iLCJ2YWx1ZSI6ImlKMEcrQkZDOGlLKzdoUStVUkNLaXBKaDl3N0RvVjBsY1NVMHMrK3hZRCs5ekRzVTg3ZTdUU1J1WHNMb3JUZ1FCWDNrRHRVOGFjdzhsY0JidzQzZEJRPT0iLCJtYWMiOiJjMDhmZmY1OGQxMGY2YWI0YmJkY2M1MzlhYWMyNzNhOTA5YjMwNThjZjM5MTBhOGJhNjJjOWYzZWVlNDY0MGQ0In0%3D; expires=Wed, 05-Oct-2016 11:18:45 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6Ik96cjBZRUJnT0daV0lyU3RLYlk2Nmc9PSIsInZhbHVlIjoidW5YMHYybUdMUktLSml0VVwvZWYxSFFTUkVKdEh3WHJQeFlNQmVYZG5aYXNhZyszQWNvY3loN09PWG1TNUJoY0RsMkRFZ0RYZE9XaXRvM1prNzNRKzRnPT0iLCJtYWMiOiI0OTVjYWRiOGIyOGNkZjM1YmFjMDczNTlmMzI4NTI5OWQ3NmZjNDhjZWFlZjQ2MDI3ZWIxZmMzZDVkNzI1ZGQ5In0%3D; expires=Wed, 05-Oct-2016 11:18:45 GMT; Max-Age=7200; path=/; httponly
Location: http://www.lemos.migueldvl.com/pt
Cache-Control: max-age=2592000
Expires: Fri, 04 Nov 2016 09:18:45 GMT
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8

HTTP/1.1 302 Found
Date: Wed, 05 Oct 2016 09:18:45 GMT
Server: Apache
X-Powered-By: PHP/5.6.26
Cache-Control: no-cache
Set-Cookie: lang=pt; expires=Fri, 04-Nov-2016 09:18:46 GMT; Max-Age=2592000; path=/
Set-Cookie: XSRF-TOKEN=eyJpdiI6InRkMFwvaFpYUENiNjdIbVRPTStuR25BPT0iLCJ2YWx1ZSI6IkZ1V2MwMk41aVYzMWVuNHBEb1JlRks0TCtmNzdxbTJIWGJYNWFldDZZXC93UlRZZmxyK3YxVmJqcDZsQm9pdFhmKzhQa0tUcXY3d0VTR2lQOW1rc1E0Zz09IiwibWFjIjoiZjcyZmI5MGJhNjk0N2Q5ZWQzM2Y4NGExNmIzNmJiNDY1NzI2NzhmMzMzZDJkMGZhMDFkMjRjMmI4M2JiNDMyMCJ9; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6IjllMDl4cjZPbEpYcWF3VktLOFVCWWc9PSIsInZhbHVlIjoiTFkrMzBMR1ZuV01aMEplMUY5M001T1wvdzZJSWIxVTZDbmdhWVwvRWp4U3pyUUZraUZhZEp1TnR2QzI0TGY0XC9JakZZbnFuNDNtdzc1eTY4c0lYVEpwYkE9PSIsIm1hYyI6Ijc3ODg5NDcyOWIyOGEzYjc4ZmNmYzExYjAyNjY0ZTg1ZmM2YjFjM2FjZDNmODBlN2EyZjJlOTQ3MmZiM2M1YmMifQ%3D%3D; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/; httponly
Location: http://www.lemos.migueldvl.com/pt
Cache-Control: max-age=2592000
Expires: Fri, 04 Nov 2016 09:18:45 GMT
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8

HTTP/1.1 200 OK
Date: Wed, 05 Oct 2016 09:18:46 GMT
Server: Apache
X-Powered-By: PHP/5.6.26
Cache-Control: no-cache
Set-Cookie: XSRF-TOKEN=eyJpdiI6ImppeXFEaFdWa3NXZlM3cG5iK3Jid0E9PSIsInZhbHVlIjoiT2pGcjFyTnN2ZStIU2tvcW53MmtKdUpudkFIOXNBT1FNQTdLbUlvbVZGbHplYnlzWHJiWW16RXVZRk0rc241Qjcwcm5RVGZTSmdOS0l0cWdDT0x1aUE9PSIsIm1hYyI6IjM0YjhlYzIyMjcwOGUzNzZkOTU5YTk2Yzc4MGI5ODNlMTkyM2QxNjg2NGUzOGVmOTM0NWFlNTNjNWNlZDg3ODMifQ%3D%3D; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6Ikt4cU5KUm9qNVhKZUxHb2dqOVlDNXc9PSIsInZhbHVlIjoiVlpZdkM5eTYzRFRGcGFaS2lOd3NsYzdsM1Y0MGt5QWlXS25WVzFJZ0k2TzlYZCtUdXZCQTVTNHpoOFNyMjdiV2pNNTFUQ1dMbGt6XC9YR1wvK2FLV2s1QT09IiwibWFjIjoiOTg2ZDMyNzMyODBiODZmMWM2ZTU2NWZlODc3ZmRkNjdiNTA4Yzg2ZDFmZTg0YmJhMzdjMTJiMDVjMWZjNTkwOCJ9; expires=Wed, 05-Oct-2016 11:18:46 GMT; Max-Age=7200; path=/; httponly
Cache-Control: max-age=2592000
Expires: Fri, 04 Nov 2016 09:18:46 GMT
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8

关于laravel - Curl 和 Laravel,总是重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39869366/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com