冷锋_百度空间
 
文章列表
 
2012/01/10 11:54 P.M.

HDWIKI 版本:5.0

环境:win + php5.2

问题:出现uc_url_unreachable错误

问题所在代码  /api/uc_client/client.php

原因:uc_client代码较久有问题,更新uc_fopen函数为:

function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
    $return = '';
    $matches = parse_url($url);
    !isset($matches['host']) && $matches['host'] = '';
    !isset($matches['path']) && $matches['path'] = '';
    !isset($matches['query']) && $matches['query'] = '';
    !isset($matches['port']) && $matches['port'] = '';
    $host = $matches['host'];
    $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
    $port = !empty($matches['port']) ? $matches['port'] : 80;
    if($post) {
        $out = "POST $path HTTP/1.0\r\n";
        $out .= "Accept: */*\r\n";
        //$out .= "Referer: $boardurl\r\n";
        $out .= "Accept-Language: zh-cn\r\n";
        $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
        $out .= "Host: $host\r\n";
        $out .= 'Content-Length: '.strlen($post)."\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Cache-Control: no-cache\r\n";
        $out .= "Cookie: $cookie\r\n\r\n";
        $out .= $post;
    } else {
        $out = "GET $path HTTP/1.0\r\n";
        $out .= "Accept: */*\r\n";
        //$out .= "Referer: $boardurl\r\n";
        $out .= "Accept-Language: zh-cn\r\n";
        $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
        $out .= "Host: $host\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Cookie: $cookie\r\n\r\n";
    }

    if(function_exists('fsockopen')) {
        $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
    } elseif (function_exists('pfsockopen')) {
        $fp = @pfsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
    } else {
        $fp = false;
    }

    if(!$fp) {
        return '';
    } else {
        stream_set_blocking($fp, $block);
        stream_set_timeout($fp, $timeout);
        @fwrite($fp, $out);
        $status = stream_get_meta_data($fp);
        if(!$status['timed_out']) {
            while (!feof($fp)) {
                if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
                    break;
                }
            }

            $stop = false;
            while(!feof($fp) && !$stop) {
                $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
                $return .= $data;
                if($limit) {
                    $limit -= strlen($data);
                    $stop = $limit <= 0;
                }
            }
        }
        @fclose($fp);
        return $return;
    }
}

 

 
2011/11/02 01:22 P.M.

 

        菠菜豆腐的危险主要是含有氰化物(Cyanide),两种氰化物混合的时候,在某些条件成立的情况下会产生剧毒,甚至毙命。自然界的青菜要含有氰化物是少见的,而菠菜和大豆都含有氰化物。貌似前些年的澳洲卫生单位就在传统小吃中检测到氰化物,追查食材才找到木薯粉含有大量的氰化物,主要是种植的环境和各种细菌的影响所致。   

        除此之外,菠菜的高量草酸也会引发急性肾衰竭,是危险的食材。正反相依是大自然的重要法则,一个食材被称之营养的好食材,同时也就是毙命的坏食材,不可能只存在单面的正面作用,而维生素,亦如是。不要说维生素,呼吸困难或心肺不行的病人给予氧气就会让细胞从休眠回到活动状态,但是不要遗忘了,导致人类会老化和死亡的主因就是氧气所带来的自由基。对于人类而言,「生」与「老」的真正剧毒物是氧气,然而氧气却是「病」的治疗剂。   牛乳醋酸物会产生腹中症结,这是蛋白质的常识,应该不用破解?   毛蟹有不少相克的,全部有其高深的医学,不是胡闹的。古时候的毛蟹,主要产地往往在重金属山脉的河川,有重金属的山川,青菜长的特别高大漂亮,毛蟹、泥鳅和一些水产都长的特别肥美,如果搭配特定的食材,就有可能产生化学变化,因而致命。   

      红李子和红柿也非常危险的,这一部分在当代的医学有大量的病人可以证明,喜欢吃这些水果的人,几乎都有胃结石。   

      欲了解食物相克图表的奥秘,必须把时空背景还原到古时候,而不是今日,今日的食材是养殖的,或人工的,而且环境已经改变了,不会有啥中毒的情况。比如古时候的中药,和今日的药效就截然不同,而食材也是相同道理,主要是环境的变迁。虽说是历史的进程,却是不容抹煞的中华思想遗产——无与伦比的古老中华文化。

 
2011/04/15 05:37 P.M.

        前些日子配置邮件服务器隔几天总被拒收,最后研究了一下,发现还要设置域名,需要设置发信IP被发送域的SPF许可。

        SPF:即Sender Policy Framework。SPF可以防止别人伪造你来发邮件,是一个反伪造性邮件的解决方案。当你定义了你的domain name的SPF记录之后,接收邮件方会根据你的SPF记录来确定连接过来的IP地址是否被包含在SPF记录里面,如果在,则认为是一封正确的邮件,否则 则认为是一封伪造的邮件。(FROM http://baike.baidu.com/view/180918.htm#sub180918

       一、示例

             dig -t txt douban.com
             可以看到TXT记录:v=spf1 ip4:173.230.147.149 ip4:209.177.154.218 ip4:211.147.4.7 include:aspmx.googlemail.com ~all
             意思是:173.230.147.149 209.177.154.218 211.147.4.7被douban.com信任,从这几个IP发送的豆瓣邮件可以被信任
             更详细的解释可以看这里:http://old.openspf.org/wizard.html?mydomain=douban.com

      二、设置
             我使用的域名设置是直接增加TXT记录,把内容写进去即可。比如增加 “v=spf1 ip4:173.230.147.149 ip4:209.177.154.218 ip4:211.147.4.7 include:aspmx.googlemail.com ~all”
 
        这是个小知识,以前没遇到过,折腾了一天

参考资料:
http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html
http://www.chinaz.com/Server/FTP/10251SK2007.html
http://baike.baidu.com/view/180918.htm#sub180918
 
2010/08/05 11:56 A.M.
注:这是一篇流水帐似的记录,仅作参考用。

          Gearman是一个分发任务的程序框架,用来实现分布式计算,一般会用来做一些运算密集型,比较耗时的工作,比如图片处理,发送email等等。

环境:

操作系统 Ubuntu 10.04       php版本 5.3

安装:

1、首先安装  Job Server(gearmand),推荐安装C语言版本

      此处  下载,解开后 ./configure; make; make install 即可。

2、安装 libgearman

       此处  下载,解开后 ./configure; make; make install 即可。

3、安装php扩展

      下载页面:http://pecl.php.net/package/gearman

      注意:我在ubuntu系统上安装时,出现错误,大家需要使用源码编译上述两个软件

      安装:首先需要有 phpize。

                 下载解压后 phpize

./configure

make

make install

             然后在php.ini中加入一行  extension=gearman.so

使用:php扩展在线帮助 http://www.php.net/manual/en/book.gearman.php
 
        示例:以下示例来自pecl中的gearman-0.7.0.tgz,我略作修改
         worker.php
            echo "Starting\n";

# Create our worker object.

$gmworker= new GearmanWorker();


# Add default server (localhost).

$gmworker->addServer();


# Register function "reverse" with the server. Change the worker function to

# "reverse_fn_fast" for a faster worker with no output.

$gmworker->addFunction("reverse", "reverse_fn_fast");


print "Waiting for job...\n";

while($gmworker->work())

{

 if ($gmworker->returnCode() != GEARMAN_SUCCESS)

 {

   echo "return_code: " . $gmworker->returnCode() . "\n";

   break;

 }

}

# A much simpler and less verbose version of the above function would be:

function reverse_fn_fast($job)

{

  sleep(10);  //睡眠

 return strrev($job->workload());

}

运行命令:php  work.php


        reverse_client_bg.php

/* create our object */

$gmclient= new GearmanClient();

/* add the default server */

$gmclient->addServer();

/* run reverse client */

$job_handle = $gmclient->doBackground("reverse", "this is a test");

echo "ok\n";

if ($gmclient->returnCode() != GEARMAN_SUCCESS)

{

 echo "bad return code\n";

 exit;

}

exit;


注:doBackground  Runs a task in the background, returning a job handle which can be used to get the status of the running task.

       所以会立刻输出 ok。


reverse_client.php


echo "Starting\n";

# Create our client object.

$gmclient= new GearmanClient();

# Add default server (localhost).

$gmclient->addServer();

echo "Sending job\n";

# Send reverse job

do

{

 $result= $gmclient->do("reverse", "Hello!");

 # Check for various return packets and errors.

 switch($gmclient->returnCode())

 {

   case GEARMAN_WORK_DATA:

     echo "Data: $result\n";

     break;

   case GEARMAN_WORK_STATUS:

     list($numerator, $denominator)= $gmclient->doStatus();

     echo "Status: $numerator/$denominator complete\n";

     break;

   case GEARMAN_SUCCESS:

     break;

   default:

     echo "RET: " . $gmclient->returnCode() . "\n";

     exit;

 }

}

while($gmclient->returnCode() != GEARMAN_SUCCESS);

echo "Success: $result\n";


注:这个脚本会在前台运行等待work返回结果,然后输出结果。所以可以用来做一些即时的图片压缩操作等等。

gearman是一个分布式计算的利器,它主要用来分发任务,它提供了一个思想,看你怎么用了。

参考资料:

http://timyang.net/linux/gearman-monitor/

http://www.php.net/manual/en/book.gearman.php

http://gearman.org/

PHP扩展中的示例

 
2010/07/16 09:55 A.M.
           近来忙于开发,近于封闭,博客已是芳草萋萋。今日记下点滴。

Haproxy是常用的七层负载软件,其调度算法也较Nginx更多。因需要将同一类的请求负载到后端的一台cache Server上,故而选用Haproxy。

主页:http://haproxy.1wt.eu/
下载:需要使用1.4版本。

安装:
                   tar -zxvf haproxy-1.4.8.tar.gz
                   cd haproxy-1.4.8/
                   make TARGET=linux26 PREFIX=/usr/local/haprpxy  
                   make install PREFIX=/usr/local/haproxy

注:
                  To build haproxy, you have to choose your target OS amongst the following ones
and assign it to the TARGET variable :

- linux22     for Linux 2.2
- linux24     for Linux 2.4 and above (default)
- linux24e    for Linux 2.4 with support for a working epoll (> 0.21)
- linux26     for Linux 2.6 and above
- solaris     for Solaris 8 or 10 (others untested)
- freebsd     for FreeBSD 5 to 8.0 (others untested)
- openbsd     for OpenBSD 3.1 to 4.6 (others untested)
- cygwin      for Cygwin
- generic     for any other OS.
- custom      to manually adjust every setting

示例要求:将 http://yourdomain/file1 , http://yourdomain/file1_1  , http://yourdomain/file1_2 等如此url定位到一台cache server

配置(简要部分):

defaults  //默认配置

mode http //默认采用http模式,可以配置tcp来做4层消息转发
option httplog //采用http日志格式
retries 3 //三次连接失败就认为是服务器不可用,主要是通过后面的check配置来实现服务器状态检查
maxconn 2000 //最大连接数
contimeout 5000 //连接超时时间
clitimeout 50000 //客户端连接超时时间
srvtimeout 50000 //服务端连接超时时间
stats uri /stats //服务器状态统计查看页面


frontend picture

bind 192.168.0.2:80
mode http
log  global
option httplog
default_backend  pic_server


backend pic_server //后端节点定义

mode http
balance uri len 6        // hash截取的uri长度
server  pic1 192.168.0.4:80   check
server  pic2  192.168.0.5:80   check

备注:这是官方文档对balance的uri调度算法的解释。
uri         The left part of the URI (before the question mark) is hashed
and divided by the total weight of the running servers. The
result designates which server will receive the request. This
ensures that a same URI will always be directed to the same
server as long as no server goes up or down. This is used
with proxy caches and anti-virus proxies in order to maximize
the cache hit rate. Note that this algorithm may only be used
in an HTTP backend. This algorithm is static by default,
which means that changing a server's weight on the fly will
have no effect, but this can be changed using "hash-type".

This algorithm support two optional parameters "len" and
"depth", both followed by a positive integer number. These
options may be helpful when it is needed to balance servers
based on the beginning of the URI only. The "len" parameter
indicates that the algorithm should only consider that many
characters at the beginning of the URI to compute the hash.
Note that having "len" set to 1 rarely makes sense since most
URIs start with a leading "/".

The "depth" parameter indicates the maximum directory depth
to be used to compute the hash. One level is counted for each
slash in the request. If both parameters are specified, the
evaluation stops when either is reached.
参考资料:

http://blog.csdn.net/cenwenchu79/archive/2009/08/04/4409343.aspx
http://blog.csdn.net/cenwenchu79/archive/2009/08/04/4409300.aspx
http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
http://bbs.linuxtone.org/thread-1079-1-2.html
 
     
 
 
文章分类
 
     
 
我的搜藏
 
     

帮助中心  |  空间客服  |  投诉中心  |  空间协议
©2012 Baidu