php monolog 的写日志到unix domain socket 测试终于成功
在另外一个客户端执行 php s.php后, 通过nc -lU /tmp/tg.sck 建立的unix domain socket 有接收到消息。

<?php
require 'vendor/autoload.php'; use Monolog\Logger;
use Monolog\Handler\SocketHandler; // Create the logger
$logger = new Logger('my_logger'); // Create the handler
$handler = new SocketHandler('unix:///tmp/tg.sck');
$handler->setPersistent(true); // Now add the handler
$logger->pushHandler($handler, Logger::DEBUG); // You can now use your logger
$logger->info('My logger is now ready');
--------------------------------------------------------------------------
php (or python) listen to unix domain stream socket
I need to create a script that listens to a unix socket and forward the incoming stream to a bot. The scripts are unable to connect. The issues seems to be related to the order of things.
Proof of case
I have created a socket and I am able to write to it. In session 1, I create a listener connection
nc -lU /tmp/tg.sck
In session 2, I write to the socket.
while true; do echo "hello"; sleep 2; done | nc -U /tmp/tg.sck
The above only works if I do it in that order. ==> Writing before you have a listener results in an error.
Using scripts (does not work)
When I replace the listing process with a PHP (or Python) script, the connection is refused because the socket is not opened.
$ python test.py
Connecting...
socket.error: [Errno 111] Connection refused
or
$ php test.php
Warning: fsockopen(): unable to connect to unix:///tmp/tg.sck:-1 (Connection refused)
Changing the order of things
If I start a working listener using the command nc -lU /tmp/tg.sck then the script does not die, but the writer process does.
Listener scripts
import socket
import sys
server_address = '/tmp/tg.sck' # Analogous to TCP (address, port) pair
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(server_address)
sock.recv(512)
and the php script
$fp = fsockopen('unix:///tmp/tg.sck', -1, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
while (!feof($fp)) {
echo fgets($fp, 4096);
}
fclose($fp);
}
php monolog 的写日志到unix domain socket 测试终于成功的更多相关文章
- 由一个简单需求到Linux环境下的syslog、unix domain socket
本文记录了因为一个简单的日志需求,继而对linux环境下syslog.rsyslog.unix domain socket的学习.本文关注使用层面,并不涉及rsyslog的实现原理,感兴趣的读者可以参 ...
- Unix Domain Socket 域套接字实现
主要注意流程: STREAM SOCKET: Server : socket() ---> bind() ---> listen() ---> accept() Client: ...
- nginx、php-fpm默认配置与性能–TCP socket还是unix domain socket【转】
原文地址:https://www.cnxct.com/default-configuration-and-performance-of-nginx-phpfpm-and-tcp-socket-or-u ...
- 【转】PHP实现系统编程(四)--- 本地套接字(Unix Domain Socket)
原文:http://blog.csdn.net/zhang197093/article/details/78143687?locationNum=6&fps=1 --------------- ...
- (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误
一个困扰我两天的问题, Google和Baidu没有找到解决方法! 此文为记录这个问题,并给出原因和解决方法. 1.Unix domain socket简介 unix域协议并不是一个实际的协议族,而是 ...
- Linux下的IPC-UNIX Domain Socket【转】
本文转载自:http://blog.csdn.net/guxch/article/details/7041052 一. 概述 UNIX Domain Socket是在socket架构上发展起来的用于同 ...
- [apue] 作为 daemon, 启动 Unix Domain Socket 侦听失败?
前段时间写一个传递文件句柄的小 demo,有 server 端.有 client 端,之间通过 Unix Domain Socket 通讯. 在普通模式下,双方可以正常建立连接,当server端作为d ...
- Envoy 基础教程:使用 Unix Domain Socket(UDS) 与上游集群通信
Envoy Proxy 在大多数情况下都是作为 Sidecar 与应用部署在同一网络环境中,每个应用只需要与 Envoy(localhost)交互,不需要知道其他服务的地址.然而这并不是 Envoy ...
- monitor a local unix domain socket like tcpdump
Can I monitor a local unix domain socket like tcpdump? - Super User https://superuser.com/questions/ ...
随机推荐
- 为什么我的 app:actionViewClass="android.widget.SearchView"和app:showAsAction="ifRoom|collapseActionView"才有
http://blog.csdn.net/cdnight/article/details/48029911 <item android:id="@+id/action_search&q ...
- C#中byte类型运算
首先看下面一段代码 byte x = 1; byte y = 2; byte z = x + y; Console.WriteLine(z); 可能很多人会说显示结果是3. 其实,这段代码无法运行,因 ...
- 删除mysql中user为空用户,mysql空密码
进入mysql数据库 mysql -uroot -p 查看用户信息 select user,host ,Password from mysql.user; 如下图: 删除user为空用户 delete ...
- Binlog详解
一.介绍 binlog, 即二进制文件,他记录了MySQL所有数据的变更,并以二进制的形式存储在磁盘上 二.binlog模式 binlog有三种模式:ROW(行模式), Statement(语句模 ...
- 关于Python中包裹传参和解包裹的理解
1.包裹传参 首先思考一个问题:为什么要有包裹传参?原因包括但不仅限于以下两点:①不确定参数的个数.②希望函数定义的更加松散灵活 包裹传参分两种:包裹位置传参和包裹关键字传参.先看包裹位置传参: 在这 ...
- solr中的Tokenizer Filter
Tokenizer Tokenizer 的工作是将文本流分解为令牌,其中每个令牌(通常)是文本中字符的子序列.分析器知道它配置的字段,但 tokenizer 不是.Tokenizers 从字符流(Re ...
- UVALive - 6267 Beer Pressure
题意: 给定n个酒吧, 然后有k个学生投票今晚去哪个酒吧, 然后会有a个(a<=k)学生先投票了, 先投的票会影响后面的人投票的概率, 求每个酒吧今晚去的概率. 分析: 我们可以从最初的状态开始 ...
- LINUX常见小问题汇总
1. crontab的备份与恢复 备份crontab文件: crontab -l > $HOME/mycron 恢复丢失的crontab文件: 如果不小心误删了crontab文件,假设你在自己的 ...
- Android学习2013年10月11日
1.LinearLayout http://www.cnblogs.com/salam/archive/2010/10/20/1856793.html LinearLayout是线性布局控件,它包含的 ...
- python 中range和xrange的区别
range() 相当于直接构造一个列表,而xrange() 是返回一个迭代值. range用法: range( 开始值,结束值,步长) 在需要大量迭代的时候,比较适合使用xrange()
