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/ ...
随机推荐
- Windows 7桌面显示图标窗口句柄的获取
在windows XP时代,我们获取桌面图标窗口的句柄往往用一下语句: HWND hwndParent = ::FindWindow( "Progman", "Progr ...
- 搜索 || BFS || POJ 2157 Maze
走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...
- docker guide
centos docker community version install: yum -y install docker # install docker systemctl start dock ...
- IOS学习笔记1—Iphone程序运行流程
Iphone程序运行流程 main.m文件,iOS应用程序的主入口 main函数的两个参数为命令行参数,在ios开发中不会用到这些元素,包括这两个参数是为了与标准ANSI C保持一致 UIApplic ...
- CF550 DIV3
A - Diverse Strings CodeForces - 1144A A string is called diverse if it contains consecutive (adjace ...
- 条款36:绝不重新定义继承而来的non-virtual函数(Never redefine an inherited non-virtual function)
NOTE: 1.绝对不要重新定义继承而来的non-virtual函数.
- SVN服务器的部署与安装
需要下载并安装VisualSVN,TortoiseSVN,VisualSVN-Server三个工具. 其中VisualSVN是SVN针对VisualStudio的插件: TortoiseSVN是客户端 ...
- 关于hibernate中的 lazy="false“
如上图所示,warehousePicked 中包含了warehousePicking 同时,数据库中warehousePicking存在数据 ,但是在debug 时warehousePicked的be ...
- Http中cookie的使用以及用CookieManager管理cookie
前段时间项目需要做个接口,接口需要先登录才能进行下一步操作,这里就需要把登录的信息携带下去,进行下一步操作.网上查了很多资料,有很多种方法.下面就介绍较常用 的. 第一种方式: 通过获取头信息的方式获 ...
- MyBatis使用Mapper动态代理开发Dao层
开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...
