重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import
站在用户的角度思考问题,与客户深入沟通,找到天坛街道网站设计与天坛街道网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广、主机域名、雅安服务器托管、企业邮箱。业务覆盖天坛街道地区。
socket
s
=
socket.socket(socket.af_inet,
socket.sock_stream)
s.bind(('127.0.0.1',
8888))
s.listen(1)
conn,
addr
=
s.accept()
conn,
addr
#
('127.0.0.1',
2134)
#addr
第一个为客户端ip,第二个为端口,上面的程序是tcp服务,udp类似。
一个tcp包只有1.4k左右(有说新版协议中可达64k),socket本身就是对tcp包连续操作了
底层协议不需要考虑,况且在php层面你也触碰不到
PHP使用socket发送HTTP请求的方法,具体如下:
socket方式:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=20, "usec"=0));socket_connect($socket, '', 80);//里面的换行代表 \r\n 注意拷贝的代码后面可能有空格$http = eof
GET / HTTP/1.0
Accept: */*
User-Agent: Lowell-Agent
Host:
Connection: Close
eof;
socket_write($socket, $http, strlen($http));while($str = socket_read($socket, 1024))
{
echo $str;
}
socket_close($socket);
fsockopen方式:
$fp = fsockopen("", 80, $errno, $errstr, 30);if (!$fp) {
echo "$errstr ($errno)br /\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: \r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $http);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
原始socket方式:
$fp = stream_socket_client("tcp://", $errno, $errstr, 30);if (!$fp) {
echo "$errstr ($errno)br /\n";
} else {
$http = eof
GET / HTTP/1.0
Accept: */*
User-Agent: Lowell-Agent
Host:
Connection: Close
eof;
fwrite($fp, $http);
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
stream 方式(get):
$http = eof
Host:
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3Cookie: BAIDUID=79D98B1AD8436C57B967E111E484F1CD:FG=1; BDUSS=lF-UFFOanFPVG92NmF4U3NiTEoxOFh4YVBCTnZaMUtoTUNhZmxrWThwN25IaUJVQVFBQUFBJCQAAAAAAAAAAAEAAADzo1gKc2lxaW5pYW8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOeR-FPnkfhTU; BAIDUPSID=79D98B1AD8436C57B967E111E484F1CD; BD_UPN=13314352; BD_HOME=1; H_PS_PSSID=10047_1435_10874_10212_10501_10496_10753_10796_10219_10355_10666_10597_10095_10658_10442_10700_10460_10360_10618; sug=3; sugstore=0; ORIGIN=2; bdime=0Connection: keep-alive
Cache-Control: max-age=0
eof;
$hdrs = array(
'http' =array(
'header' = $http,
'timeout'=1, //超时 秒
'method' = 'GET', //默认方式
'protocol_version' = '1.1', //默认为 1.0),
);
//参数格式参考 方式的格式可以参考; = stream_context_create($hdrs);
echo file_get_contents('', 0, $context);stream 方式 post:
$postdata = http_build_query(array('act'='save', 'id'=387171));$http = eof
Host:
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3Content-Type: application/x-www-form-urlencoded; charset=UTF-8Cookie: BAIDUID=79D98B1AD8436C57B967E111E484F1CD:FG=1; BDUSS=lF-UFFOanFPVG92NmF4U3NiTEoxOFh4YVBCTnZaMUtoTUNhZmxrWThwN25IaUJVQVFBQUFBJCQAAAAAAAAAAAEAAADzo1gKc2lxaW5pYW8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOeR-FPnkfhTU; BAIDUPSID=79D98B1AD8436C57B967E111E484F1CD; BD_UPN=13314352; BD_HOME=1; H_PS_PSSID=10047_1435_10874_10212_10501_10496_10753_10796_10219_10355_10666_10597_10095_10658_10442_10700_10460_10360_10618; sug=3; sugstore=0; ORIGIN=2; bdime=0Connection: keep-alive
Cache-Control: max-age=0
eof;
#注意post方式需要增加Content-Type
$hdrs = array(
'http' =array(
'header' = $http,
'timeout'=1, //超时 秒
'method' = 'POST',
'content' = $postdata,
'protocol_version' = '1.1', //默认为 1.0),
);
//参数格式参考 方式的格式可以参考; = stream_context_create($hdrs);
echo file_get_contents('', 0, $context);
这个可以用AJAX技术啦,,用JS去取得内容,document.getElementById();这样取得内容后再传给一个处理的PHP文件.然后再使用JS去返回到HTML页面里就可以了. 简单吧///