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