UDP server - client를 구현중 python 3.2 에서는 이상한 에러를 자주 뱉어 내게 된다.
이이유는 sendto가 2.7에서는 string 으로 전송이 가능하였지만 3.2 부터는 바이트로 전송이 되어야 하기 때문이다.
예제코드는 아래와 같다
[ server ]
def socketServer(): svrsock = socket(AF_INET,SOCK_DGRAM) svrsock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) try: svrsock.bind(('',34444)) except IOError: print ("Socket bindding error plz use another port \n") while 1: try : s,addr = svrsock.recvfrom(100) print(s) svrsock.sendto(bytes(ALTERT_MESSAGE,'ascii'),addr) //여기가 오류를 뱉어낸 부분이다. except IOError: print ('socket sendto error') |
[ client ]
import socket HOST = '127.0.0.1' # The remote host PORT = 34444 # The same port as used by the server sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(bytes("aaa",'ascii'), (HOST, PORT)) buff=sock.recvfrom(100) print(buff) |
'Python' 카테고리의 다른 글
[ python 3.2 ] thread -> _thread (0) | 2012.07.18 |
---|---|
stdout 으로 표준 출력 되는 값을 변수로 저장하여 가져오기. (0) | 2012.07.18 |
[ python ] LIST 미리 선언해주기 (0) | 2012.07.16 |
[ python ] 파일을 뒤에서 부터 불러오고 싶을 때.. seek 오류관련 (0) | 2012.07.16 |
[ python ] webhacking.kr를 위한 본 파이썬 블라인드 인젝터 코드 (컬럼명을 알았을 시) (0) | 2012.07.12 |