我有一个 PHP 脚本,它应该连接到一个从代理列表中选择的代理并下载一个文件。一些代理(在 200-400 个工作代理中)工作完美,但其他的则不能,我无法找出原因。
这里是通过代理连接的代码:
$proxy = determine_proxy ($proxyList);
$proxyString = 'tcp://' . $proxy['ip'] . ':' . $proxy['port'];
$userAgent = $userAgents [rand (0, $agentsCount - 1)];
// set up our headers
$hdrs = array( 'http' => array(
'method' => "GET",
'header'=> "Host: www.example.net\r\n" .
// "User-Agent: $userAgent\r\n" .
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
"Accept-Language: en-us,en;q=0.5\r\n" .
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
"Keep-Alive: 115\r\n" .
"Proxy-Connection: keep-alive\r\n" .
"Referer: http://$url", // Setting the http-referer
'proxy' => "$proxyString",
'request_fulluri' => true
)
);
echo "Using proxy: "; print_r ($proxy); echo '<br>';
$context = stream_context_create ($hdrs); // set up the context
$timeout = 3;
$oldTimeout = ini_set('default_socket_timeout', $timeout);
$oldAgent = ini_set ('user_agent', $userAgent);
$fp = fopen ("http://www.example.net$file", 'r', false, $context); // open the file
if (!$fp) {
echo 'fopen failed! Skipping this proxy for now...<br>';
print_r ($http_response_header); echo '<br />';
unset ($http_response_header);
flush(); @ob_flush();
ini_set ('user_agent', $oldAgent);
ini_set('default_socket_timeout', $oldTimeout);
continue;
}
print_r ($http_response_header); echo '<br />';
unset ($http_response_header);
奇怪的是,失败尝试的响应头有时是空的,有时是这样的:
Array (
[0] => HTTP/1.0 200 OK
[1] => Server: falcon
[2] => Date: Sun, 16 Jan 2011 14:06:37 GMT
[3] => Content-Type: application/x-bittorrent
[4] => Cache-Control: must-revalidate, post-check=0, pre-check=0
[5] => Content-Disposition: attachment; filename="example.torrent"
[6] => Vary: Accept-Encoding,User-Agent
[7] => Connection: close
)
有时,是这样的:
Array (
[0] => HTTP/1.0 200 OK
[1] => Server: falcon
[2] => Date: Sun, 16 Jan 2011 14:06:47 GMT
[3] => Content-Type: application/x-bittorrent
[4] => Cache-Control: must-revalidate, post-check=0, pre-check=0
[5] => Content-Disposition: attachment; filename="example2.torrent"
[6] => Vary: Accept-Encoding,User-Agent
[7] => X-Cache: MISS from proxy
[8] => Proxy-Connection: close
)
这是来自成功尝试的响应 header :
HTTP/1.0 200 OK
Server: falcon
Date: Fri, 21 Jan 2011 18:53:00 GMT
Content-Type: application/x-bittorrent
Cache-Control: must-revalidate, post-check=0, pre-check=0
Content-Disposition: attachment; filename="example3.torrent"
Vary: Accept-Encoding,User-Agent
X-Cache: MISS from www.example.com
X-Cache-Lookup: MISS from www.example.com:3128
Via: 1.0 www.example.com (squid/3.0.STABLE23-BZR)
Proxy-Connection: close
我正在将用户代理设置为有效的用户代理字符串,我已检查 allow_url_fopen 并将其设置为开。
来自 RFC-2616,第 10 节:
200 OK
The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:
GET an entity corresponding to the requested resource is sent in the response;
怎么可能,服务器通过代理返回状态 200,而 fopen 仍然失败?有人知道这个问题以及如何解决吗?
最佳答案
问题在于,我设置的套接字超时在某些情况下对于 fopen 来说太低,无法管理和下载所有数据。超时时间结束后,fopen 仍未下载数据,它返回 FALSE 并抛出“HTTP 请求失败”错误。
关于PHP:fopen 失败 "HTTP Request Failed",但响应 header 的状态代码为 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4705902/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r