获取设备名称和IPv4地址


socket.gethostname()
socket.gethostbyname(host_name)
 
 def print_machine_info():
host_name = socket.gethostname();
ip_address = socket.gethostbyname(host_name)
print ('host_name : ',host_name)
print (host_name,'\'IP : ',ip_address)

获取远程设备的IP地址


socket.gethostbyname(remote_host)
 def get_remote_machine_info(remote_host):
try:
ip_address = socket.gethostbyname(remote_host)
print (remote_host,'\'IP : ',ip_address)
except (socket.error) as err_msg:
print (remote_host,err_msg)

通过指定的端口和协议找到服务名


socket.getservbyport(port)

 def find_service_name():
for port in [80,25,53,22]:
service = socket.getservbyport(port)
print (service)

设定并获取默认的套接字超时时间


s.gettimeout()

s.settimeout(n)

 def test_socket_timeout():
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print (s.gettimeout())
s.settimeout(20)
print (s.gettimeout())

这种操作在开发服务器应用时很有用,若把超时时间设置为None,则禁用了套接字操作的超时检测

附上一张常用端口图  常用计算机端口

inet_aton

inet_ntoa

将IPv4地址转换成不同的格式(32位二进制)

ntohl()

htonl()

主机字节序和网络字节序之间的相互转换

n(network),l(long),s(short)

最新文章

  1. Go语言http包Form解析之坑
  2. 从 A/Looper: Could not create epoll instance. errno=24 错误浅谈解决各种 bug 的思路
  3. Swift 懒加载(lazy) 和 Objective-C 懒加载的区别
  4. 一步一步学习underscore的封装和扩展方式
  5. 编译osg的一个错误
  6. Spring配置JNDI的解决方案
  7. subclipse安装后从svn资源库视图check out的资源无法创建server
  8. javascript算法汇总(持续更新中)
  9. ubuntu 下 数学库编译链接时找不到各种数学问题解决方法 can not fon atan 等等
  10. OpenSSH 'child_set_env()'函数安全绕过漏洞
  11. [Hadoop源码解读](三)MapReduce篇之Job类
  12. epoll原理解释(转)
  13. 安卓天天练练(四)drawable state 属性
  14. android手机端保存xml数据
  15. STM32与LPC系列ARM资源之比较
  16. 关于bool和BOOL的区别
  17. JS中金额转换以及格式化
  18. Docker 安装 MySQL
  19. 【数学建模】数模day13-灰色系统理论I-灰色关联与GM(1,1)预测
  20. Memorise Me!——用数值做地址,实现快速查找

热门文章

  1. 如何在Unity中显示FPS
  2. js获取站点根目录
  3. LeetCode6.Z字形变换 JavaScript
  4. TensorFlow安装环境的误区
  5. JS基础——浅谈前端页面渲染和性能优化
  6. CommonJs模块规范
  7. multimap的使用
  8. ABAP术语-Error Message
  9. ABAP术语-Accounting Document
  10. gcc 手动安装