Chinaunix首页 | 论坛 | 博客
  • 博客访问: 461246
  • 博文数量: 108
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 1134
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-29 19:43
文章分类

全部博文(108)

文章存档

2016年(10)

2015年(9)

2014年(73)

2013年(16)

我的朋友

分类: Python/Ruby

2015-01-07 18:53:30


点击(此处)折叠或打开

  1. >>> import socket
  2. >>> def print_machine_info():
  3. ... host_name=socket.gethostname()
  4. ... ip_address=socket.gethostbyname(host_name)
  5. ... print "Host name: %s" % host_name
  6. ... print "IP address: %s" % ip_address
  7. ...
  8. >>> if __name__ == '__main__':
  9. ... print_machine_info()
  10. ...
  11. Host name: 10-58-89-62
  12. IP address: 10.58.89.62
  13. >>> def get_remote_machine_info():
  14. ... remote_host="cloud.letv.com"
  15. ... try:
  16. ... print "IP address: %s" % socket.gethostbyname(remote_host)
  17. ... except socket.error,err_msg:
  18. ... print "%s: %s" % (remote_host,err_msg)
  19. ...
  20. >>> if __name__== '__main__':
  21. ... get_remote_machine_info()
  22. ...
  23. IP address: 123.126.32.228

点击(此处)折叠或打开

  1. >>> import socket
  2. >>> from binascii import hexlify
  3. >>> def convert_ip4_address():
  4. ... for ip_addr in ['127.0.0.1','192.168.0.1']:
  5. ... packed_ip_addr=socket.inet_aton(ip_addr)
  6. ... unpacked_ip_addr=socket.inet_ntoa(packed_ip_addr)
  7. ... print "IP Address: %s => Packed: %s, Unpacked: %s " % (ip_addr,hexlify(packed_ip_addr), unpacked_ip_addr)
  8. ...
  9. >>> if __name__ == "__main__":
  10. ... convert_ip4_address()
  11. ...
  12. IP Address: 127.0.0.1 => Packed: 7f000001, Unpacked: 127.0.0.1
  13. IP Address: 192.168.0.1 => Packed: c0a80001, Unpacked: 192.168.0.1


阅读(952) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~