Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import time
- from enum import Enum
- sep = '\r\n'
- TCP_IP = '127.0.0.1'
- TCP_PORT = 5008
- BUFFER_SIZE = 20
- class StreamType(Enum):
- OPEN = 1
- CLOSE = 2
- def prepareData(data):
- return (str(data.value) + sep).encode('ascii')
- def getDataBlocking(sock):
- buffer = ''
- sock.setblocking(1)
- isDataFetched = False
- while isDataFetched == False:
- buffer += str(sock.recv(BUFFER_SIZE), 'ascii')
- if sep in buffer:
- isDataFetched = True;
- return buffer.replace(sep, '')
- def getDataNonBlocking(sock):
- buffer = ''
- sock.settimeout(0.0)
- isDataFetched = False
- while isDataFetched == False:
- try:
- buffer += str(sock.recv(BUFFER_SIZE), 'ascii')
- except Exception as e:
- return None
- if sep in buffer:
- isDataFetched = True;
- return buffer.replace(sep, '')
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((TCP_IP, TCP_PORT))
- s.setblocking(0)
- s.send(prepareData(StreamType.OPEN))
- count = 0;
- while True:
- data = getDataBlocking(s)
- s.send(prepareData(StreamType.OPEN))
- time.sleep(1)
- if data != None:
- print(data)
- count += 1
- if count==20:
- break
- s.send(prepareData(StreamType.CLOSE))
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement