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/ ...
随机推荐
- hdu6290 奢侈的旅行
最短路算法的复杂度考虑! 书上已经做了优化,用的是优先队列:用优先队列实现堆优化 V为点集,E为边集 从O(V^2)优化到O(ElogV) 然后再记忆一下inf 0x3f3f3f3f的十进制是1061 ...
- T1订正记-AC自动机-从树到图
AC自动机已经足够棒了. 但是,好像有时还是要TLE的. 一般的AC自动还是比较好,如果在某些情况下还是会被卡掉,像是这个水题 考试的感觉 我看到这个题后,我清清楚楚的知道,这是个AC自动机+栈. 经 ...
- 【原创翻译】链接DLL至可执行文件---翻译自MSDN
可执行文件.exe链接(或加载)DLL有以下两种形式: 隐式链接 显式链接 隐式链接是指静态加载或在程序加载时动态链接. 通过隐式链接,在使用DLL时,可执行文件链接到一个由生成DLL的人提供的导入函 ...
- 高德地图api之location定位
关于定位,分为GPS定位和网络定位.本文将详细描述的浏览器定位,属于网络定位.这是一种通过使用高德JS-API来实现位置定位.城市定位的方法,包含了IP定位,检索等多种网络定位方式.如果您的手机支持G ...
- tiny4412学习笔记-将uboot、zImage、文件系统烧到emmc中 (转)
http://blog.chinaunix.net/uid-30025978-id-4788683.html 1.首先还是要将u-boot写入SD卡中从SD卡启动. 使用读卡器将SD插入电脑中,使用u ...
- finally块的问题(finally block does not complete normally)
http://blog.csdn.net/chh_jiang/article/details/4557461 当finall块中包含return语句时,Eclipse会给出警告“finally blo ...
- pwnable flag之write up
Papa brought me a packed present! let's open it. Download : http://pwnable.kr/bin/flag This is rever ...
- LeetCode(18)4Sum
题目 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- xtu read problem training 3 A - The Child and Homework
The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on Code ...
- 【ITOO 5】启用MSDTC后的各种问题,以及解决方案
导读:在上篇博客中说到,通过启动微软的msdtc来实现分布式事务处理,随后,问题就来了,现在,一一总结这个过程中的问题. 一.已禁用对分布式事务管理器(MSDTC)的网络访问. 1.1,问题描述 已禁 ...