本地套接字-本地socket

本地套接字简单应用场景 一
#服务端--简单
import socket
import os
a='sock_file'
if os.path.exists(a):
    os.remove(a)

s=socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
s.bind(a)
s.listen(3)
while True:
    c,addr = s.accept()
    while True:
        date=c.recv(1024)
        if not date:
            c.close()
            break
        else:
            c.send(date)
#客户端--简单
import socket,time
a='sock_file'
s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
s.connect(a)
while True:
    msg='你好1'.encode()
    s.send(msg)
    date=s.recv(1024)
    print(date.decode())
    time.sleep(2)
 

 

本地套接字循环发送信息示例应用场景2
#服务端
import socket,time,threading
import os,select
a='sock_file'
if os.path.exists(a):
    os.remove(a)

s=socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
s.setblocking(False)
s.bind(a)
s.listen()
rlist=[s,]
wlist=[]
erlist=[]
def accept_():
    while True:
        rs,ws,er=select.select(rlist,wlist,erlist)
        print(ws)
        time.sleep(2)
        print(1)
        for i in rs:
            if i is s:
                con,add=s.accept()
                rlist.append(con)

        for i in erlist:
            print(i)

def send_():
    while True:
        time.sleep(2)
        for i in rlist:
            if i is s:
                pass
            else:
                date='你好'.encode()
                try:
                    i.send(date)
                except:
                    i.close()
                    rlist.remove(i)
t1=threading.Thread(target=accept_,)
t1.start()
t2=threading.Thread(target=send_,)
t2.start()
t1.join()
t2.join()
#客户端---
import socket,time
a='sock_file'
s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
s.connect(a)
while True:
    # msg='你好2'.encode()
    # s.send(msg)
    date=s.recv(1024)
    print(date.decode())
    # time.sleep(2)

 



 

posted @ 2018-09-10 11:02  Sky__liu  阅读(1346)  评论(0编辑  收藏  举报