-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketUtils.py
53 lines (37 loc) · 1.12 KB
/
socketUtils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#socketUtils.py
import socket
import pickle
import select
MINER_SERVER_PORT = 5005
WALLET_SERVER_PORT = 5006
BUFFER_SIZE = 1024
def newServerConnection(ip_addr, port=MINER_SERVER_PORT):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((ip_addr, port))
s.listen()
return s
def recvObj(socket):
inputs, outpus, errs = select.select([socket], [], [socket], 6)
if socket in inputs:
new_sock, addr = socket.accept()
all_data = b'';
while True:
data = new_sock.recv(BUFFER_SIZE)
if not data: break
all_data += data
return pickle.loads(all_data)
return None
def sendObj(ip_addr, inObj, port=MINER_SERVER_PORT):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_addr, port))
data = pickle.dumps(inObj)
s.send(data)
s.close()
return False
if __name__ == '__main__':
server = newServerConnection('localhost')
O = recvObj(server)
print("Success!") #If returns after time, then successful!
#print(O)
server.close()