码迷,mamicode.com
首页 > 编程语言 > 详细

python之socket实现unix socket及dash字符串操作

时间:2017-06-27 22:10:22      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:socket   python   af   

     python的socket库可以实现tcp和udp,在linux下unix socket也是可以的,有些程序在进程间通信就是使用unix socket,如果我们想查看其通信的信息来进行调试,则可以用python来伪造其接口,获取内容

参考:https://docs.python.org/2/library/socket.html



import socket
import os 
if __name__ == ‘__main__‘:     
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)  
    if os.path.exists(‘/tmp/UNIX.d‘):  
        os.unlink(‘/tmp/UNIX.d‘)  
    sock.bind(‘/var/run/rrdcached.sock‘)     
    sock.listen(5)     
    while True:  
       connection,address = sock.accept()      
       print "Data : %s"%connection.recv(1024) 
       #connection.send("hello")  
       connection.close()


参考:http://www.2cto.com/os/201305/210033.html

bash下字符串操作: 

支持${parameter:offset:length},${parameter:offset}

 

igi@gentoo ~/test $ string=‘hello‘

igi@gentoo ~/test $ echo ${string:1:3}

ell

igi@gentoo ~/test $ echo ${string:1}

ello

igi@gentoo ~/test $ echo $0

/bin/bash


dash: 不支持, 替代方法:采用expr或cut外部命令代替

 

$ string=‘hello‘

$ expr substr "$string" 2 3

ell

$ echo "$string" | cut -c2-4

ell

$ expr substr "$string" 2 "${#string}"

ello

$ echo "$string" | cut -c2-

ello

$ echo $0

dash

$


本文出自 “DanielQu” 博客,请务必保留此出处http://qujunorz.blog.51cto.com/6378776/1942404

python之socket实现unix socket及dash字符串操作

标签:socket   python   af   

原文地址:http://qujunorz.blog.51cto.com/6378776/1942404

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!