forked from Malric/NMPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
75 lines (69 loc) · 2.09 KB
/
helpers.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import datetime
import errno
import os
import socket
##
# Binding helper
##
def bind(PORT):
""" Create UDP socket and bind given port with it. """
#HOST = '127.0.0.1' # Local host
HOST = socket.gethostbyname(socket.getfqdn())
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_DGRAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
print 'Streamer: '+str(msg)
s = None
continue
try:
s.bind(sa)
except socket.error as msg:
print 'Streamer: '+str(msg)
s.close()
s = None
continue
break
return s
##
# Connects to google dns and gets public ethernet interface ip address of the machine from socket.
##
def tcpLocalIp():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ret = s.getsockname()[0]
s.close()
return ret
##
# Creates and binds udp socket. Creates another udp socket and sends to binded socket message. Address information is found in the udp recvfrom function
##
def udpLocalIp():
server_socket = bind(12345)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto("Ping!",(socket.gethostname(),12345))
data, address = server_socket.recvfrom(1024)
ret = address[0]
sock.close()
server_socket.close()
return ret
##
# Gets public ethernet interface ip address of the machine using dns
##
def sockLocalIp():
ret = socket.gethostbyname(socket.getfqdn())
return ret
##
# Convinient timestamp function
##
def getTimestamp():
time = datetime.datetime.today()
timestamp = str(time.year)+"_"+str(time.month)+"_"+str(time.day)+"_"+str(time.hour)+"_"+str(time.minute)+"_"+str(time.second)
return timestamp
def createDir(name):
try:
os.makedirs(os.getcwd() + "/" +name) # create dir to current working dir
except OSError as exception:
if exception.errno != errno.EEXIST: # ignore error if path exists
raise