Curl和Laravel,总是重定向

时间:2016-10-05 08:48:37

标签: laravel curl cookies laravel-5 python-requests

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

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

结果如下:

请求标题

*   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

响应标题和正文

< 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:我还在-L使用了curl标记,因此它可以暂停重定向,但它似乎最终会进入无限循环(curl: (47) Maximum (50) redirects followed

为什么这是hapening?怎么解决?

1 个答案:

答案 0 :(得分:4)

您正在设置Cookie,如果该Cookie不存在,您可以使用302 Found将用户重定向到同一页面。具体而言,您需要检查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