-
>>> import socket
-
>>> def print_machine_info():
-
... host_name=socket.gethostname()
-
... ip_address=socket.gethostbyname(host_name)
-
... print "Host name: %s" % host_name
-
... print "IP address: %s" % ip_address
-
...
-
>>> if __name__ == '__main__':
-
... print_machine_info()
-
...
-
Host name: 10-58-89-62
-
IP address: 10.58.89.62
-
>>> def get_remote_machine_info():
-
... remote_host="cloud.letv.com"
-
... try:
-
... print "IP address: %s" % socket.gethostbyname(remote_host)
-
... except socket.error,err_msg:
-
... print "%s: %s" % (remote_host,err_msg)
-
...
-
>>> if __name__== '__main__':
-
... get_remote_machine_info()
-
...
-
IP address: 123.126.32.228
-
>>> import socket
-
>>> from binascii import hexlify
-
>>> def convert_ip4_address():
-
... for ip_addr in ['127.0.0.1','192.168.0.1']:
-
... packed_ip_addr=socket.inet_aton(ip_addr)
-
... unpacked_ip_addr=socket.inet_ntoa(packed_ip_addr)
-
... print "IP Address: %s => Packed: %s, Unpacked: %s " % (ip_addr,hexlify(packed_ip_addr), unpacked_ip_addr)
-
...
-
>>> if __name__ == "__main__":
-
... convert_ip4_address()
-
...
-
IP Address: 127.0.0.1 => Packed: 7f000001, Unpacked: 127.0.0.1
-
IP Address: 192.168.0.1 => Packed: c0a80001, Unpacked: 192.168.0.1
阅读(1059) | 评论(0) | 转发(0) |