-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgetname.py
More file actions
29 lines (24 loc) · 899 Bytes
/
getname.py
File metadata and controls
29 lines (24 loc) · 899 Bytes
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
import httplib
import socket
from xml.dom.minidom import parseString, Node
def getmachinename():
hostname = socket.gethostname().split('.')[0]
try:
svr = 'ip-address.domaintools.com'
url = '/myip.xml'
conn = httplib.HTTPConnection(svr)
conn.request("GET", url)
response = conn.getresponse()
data = response.read()
if response.status == 200:
doc = parseString(data)
for node in doc.getElementsByTagName("hostname"):
for i in node.childNodes:
if i.nodeType == Node.TEXT_NODE:
return ".".join((hostname, i.data))
else:
raise Exception (response.msg + '\n' + data)
except:
return hostname
if __name__ == "__main__":
print getmachinename()