select 를 사용 하여 비 차단 socket | dbafree 첫 페이지 구현
1969 단어 socket
Liux, perl, python 에 모두 select 시스템 호출 이 존재 합 니 다.다음 두 개의 python 프로그램 은 select 작업 을 배우 고 디 버 깅 할 수 있 습 니 다.
참고:http://blog.chinaunix.net/space.php?uid=199788&do=blog&id=99434
1. 서버 코드
#!/home/oracle/dbapython/bin/python
# -*- coding: utf-8 -*-
"""
socket ( ubuntu linux windows xp ,Windows select.select)
socket list:in/out/err
5000ms , , ; ,
5000ms "no data coming"
"""
import socket,select
host="";
port=50000;
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port));
s.listen(5);
while 1:
infds,outfds,errfds = select.select([s,],[],[],0.1)
# infds , ,
if len(infds) != 0:
clientsock,clientaddr = s.accept()
buf = clientsock.recv(8196)
if len(buf) != 0:
print (buf)
clientsock.close()
print "no data coming"
2. 클 라 이언 트 코드
#!/home/oracle/dbapython/bin/python
# -*- coding: utf-8 -*-
import socket,select
host = "localhost"
port = 50000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# , , select()
#FD_ISSET(sockfd) , !
# server accept
s.connect((host,port))
# send, ,server accept ,
s.send("coming from select client by dbafree")
s.close()
왜 select 로 이 루어 져 야 합 니까? accept 는 차단 식 이기 때문에 accept 를 실행 하면 계속 기다 릴 것 입 니 다.
물론 이런 방식 은 말하자면 select 로 연결 을 감청 하 는 것 이다.연결 이 만들어 지면 accept () 함 수 는 후속 통신 에 사용 할 단독 클 라 이언 트 socket 을 되 돌려 줍 니 다. 이때 클 라 이언 트 측 이 데 이 터 를 보 내지 않 으 면 server 측 도 계속 기다 릴 것 입 니 다.개인 적 으로 이해 합 니 다.
기타 관련 자료 man select python doc 의 설명:http://docs.python.org/library/select.html?highlight=select.select#select.select linux 에서 선택:http://digdeeply.info/archives/10121463.html