python socket编程2 - socket创建发送方所需参数的获得

news2024/10/6 18:27:03

使用socket进行进程间通信或者跨网络的计算机间通讯,有点类似日常生活中的发送快递。

根据发送方的需要,选择不同的物流公司:
在这里插入图片描述
在选择适合的公司和运输方式后,需要在app上做出选择,并根据要求填写一些信息。app会根据填写的信息,判断和提示是否可行。

比如,有液体的物品是不可以走空运的;
比如,当日达的物品是不能做到隐去发送方地址的;
比如,运送的物品重量大于3公斤,需要额外收取费用的;
比如,发送方不在同一个城市,当日达可能就无法使用。

发送双方的位置、以及寄送物品的某些性质,决定了可能选择的运送公司、运送方式、时限以及费用等。

socket编程也是如此。

发送方需要先确定一些参数,还要知道接受方的一些参数,然后选择合适的协议等内容。

好在 python socket 提供了一些可以直接使用的方法,方便使用者通过简单的方法调用,获得发送方需要使用的一些数据。

方法列举

  • def close(integer):
    close(integer) -> None
    
    Close an integer socket file descriptor.  
    This is like os.close(), but for sockets; 
    on some platforms os.close() won't work for socket file descriptors.
  • def dup(integer):
    dup(integer) -> integer
    
    Duplicate an integer socket file descriptor.  This is like os.dup(), but for  sockets; 
    on some platforms os.dup() won't work for socket file descriptors.
  • def getaddrinfo(host, port, family=None, type=None, proto=None, flags=None):
   getaddrinfo(host, port [, family, type, proto, flags])   -> list of (family, type, proto, canonname, sockaddr)
    
    Resolve host and port into addrinfo struct.

参考网址: https://www.man7.org/linux/man-pages/man3/getaddrinfo.3.html

  • def getdefaulttimeout():
    getdefaulttimeout() -> timeout
    
    Returns the default timeout in seconds (float) for new socket objects.
    A value of None indicates that new socket objects have no timeout.
    When the socket module is first imported, the default is None.
    默认是None.
  • def gethostbyaddr(host):
    gethostbyaddr(host) -> (name, aliaslist, addresslist)
    
    Return the true host name, a list of aliases, and a list of IP addresses, for a host.  
    The host argument is a string giving a host name or IP number.
  • def gethostbyname(host):
    gethostbyname(host) -> address
    
    Return the IP address (a string of the form '255.255.255.255') for a host.
  • def gethostbyname_ex(host):
    gethostbyname_ex(host) -> (name, aliaslist, addresslist)
    
    Return the true host name, a list of aliases, and a list of IP addresses, for a host.  
    The host argument is a string giving a host name or IP number.
  • def gethostname():
    gethostname() -> string
    
    Return the current host name.
  • def getnameinfo(sockaddr, flags):
    getnameinfo(sockaddr, flags) --> (host, port)
    
    Get host and port for a sockaddr.
  • def getprotobyname(name):
    getprotobyname(name) -> integer
    
    Return the protocol number for the named protocol.  (Rarely used.)
  • def getservbyname(servicename, protocolname=None):
    getservbyname(servicename[, protocolname]) -> integer
    
    Return a port number from a service name and protocol name.
    The optional protocol name, if given, should be 'tcp' or 'udp',   otherwise any protocol will match.
  • def getservbyport(port, protocolname=None):
    getservbyport(port[, protocolname]) -> string
    
    Return the service name from a port number and protocol name.
    The optional protocol name, if given, should be 'tcp' or 'udp', otherwise any protocol will match.
  • def htonl(integer):
    htonl(integer) -> integer
    
    Convert a 32-bit integer from host to network byte order.
  • def htons(integer):
    htons(integer) -> integer
    
    Convert a 16-bit unsigned integer from host to network byte order.
    Note that in case the received integer does not fit in 16-bit unsigned integer, 
    but does fit in a positive C int, it is silently truncated to 16-bit unsigned integer.
    However, this silent truncation feature is deprecated, and will raise an  exception in future versions of Python.
  • def if_indextoname(if_index):
    if_indextoname(if_index)
    
    Returns the interface name corresponding to the interface index if_index.
  • def if_nameindex():
    if_nameindex()
    
    Returns a list of network interface information (index, name) tuples.
  • def if_nametoindex(if_name):
    if_nametoindex(if_name)
    
    Returns the interface index corresponding to the interface name if_name.
  • def inet_aton(string):
    inet_aton(string) -> bytes giving packed 32-bit IP representation
    
    Convert an IP address in string format (123.45.67.89) to the 32-bit packed  binary format used
    in low-level network functions.
  • def inet_ntoa(packed_ip):
    inet_ntoa(packed_ip) -> ip_address_string
    
    Convert an IP address from 32-bit packed binary format to string format
  • def inet_ntop(af, packed_ip):
    inet_ntop(af, packed_ip) -> string formatted IP address
    
    Convert a packed IP address of the given family to string format.
  • def inet_pton(af, ip):
    inet_pton(af, ip) -> packed IP address string
    
    Convert an IP address from string format to a packed string suitable  for use with low-level network functions.
  • def ntohl(integer):
    ntohl(integer) -> integer
    
    Convert a 32-bit integer from network to host byte order.
  • def ntohs(integer):
    ntohs(integer) -> integer
    
    Convert a 16-bit unsigned integer from network to host byte order.
    Note that in case the received integer does not fit in 16-bit unsigned  integer, but does fit in a positive C int,
     it is silently truncated to  16-bit unsigned integer.
    However, this silent truncation feature is deprecated, and will raise an  exception in future versions of Python.
  • def setdefaulttimeout(timeout):
    setdefaulttimeout(timeout)
    
    Set the default timeout in seconds (float) for new socket objects.
    A value of None indicates that new socket objects have no timeout.
    When the socket module is first imported, the default is None.

代码举例

  • 获得本地主机信息
def print_localhost_info():
    host_name = socket.gethostname()
    ip_addr = socket.gethostbyname(host_name)
    print("Host name: %s " % host_name)
    print("IP address: %s" % ip_addr)

Host name: DESKTOP-DEVTEAM
IP address: 192.168.56.1

  • 获得外网站点信息
def print_remote_website_info():
    remote_host = 'www.pythons.org'
    try:
        print("IP address: %s" % socket.gethostbyname(remote_host))
    except socket.error as err_msg:
        print("%s: %s" % (remote_host, err_msg))

IP address: 72.14.178.174

  • 输出默认超时时间
def print_default_timeout():
    print("Default timeout :", socket.getdefaulttimeout())

Default timeout : None

  • 根据网址获得主机名、别名列表、IP列表
def print_host_by_addr():
    print("Host address :", socket.gethostbyaddr('www.pythons.org'))

Host address : (‘li40-174.members.linode.com’, [], [‘72.14.178.174’])

  • 输出本地主机网卡信息
def print_network_interface():
    print(socket.if_nameindex())

[(22, ‘ethernet_0’), (23, ‘ethernet_1’), (24, ‘ethernet_2’), (25, ‘ethernet_3’), (26, ‘ethernet_4’), (27, ‘ethernet_5’), (28, ‘ethernet_6’), (29, ‘ethernet_7’), (30, ‘ethernet_8’), (31, ‘ethernet_9’), (32, ‘ethernet_10’), (33, ‘ethernet_11’), (43, ‘ethernet_12’), (44, ‘ethernet_13’), (45, ‘ethernet_14’), (46, ‘ethernet_15’), (47, ‘ethernet_16’), (48, ‘ethernet_17’), (49, ‘ethernet_18’), (50, ‘ethernet_19’), (51, ‘ethernet_20’), (12, ‘ethernet_32768’), (15, ‘ethernet_32769’), (2, ‘ethernet_32770’), (9, ‘ethernet_32771’), (21, ‘ethernet_32772’), (7, ‘ethernet_32773’), (14, ‘ethernet_32774’), (20, ‘ethernet_32775’), (6, ‘ethernet_32776’), (8, ‘ppp_32768’), (1, ‘loopback_0’), (34, ‘wireless_0’), (35, ‘wireless_1’), (36, ‘wireless_2’), (37, ‘wireless_3’), (38, ‘wireless_4’), (39, ‘wireless_5’), (40, ‘wireless_6’), (41, ‘wireless_7’), (42, ‘wireless_8’), (52, ‘wireless_9’), (53, ‘wireless_10’), (54, ‘wireless_11’), (55, ‘wireless_12’), (56, ‘wireless_13’), (57, ‘wireless_14’), (58, ‘wireless_15’), (59, ‘wireless_16’), (60, ‘wireless_17’), (61, ‘wireless_18’), (18, ‘wireless_32768’), (19, ‘wireless_32769’), (11, ‘wireless_32770’), (13, ‘tunnel_32512’), (5, ‘tunnel_32513’), (3, ‘tunnel_32514’), (16, ‘tunnel_32768’), (4, ‘tunnel_32769’), (17, ‘tunnel_32770’), (10, ‘tunnel_32771’)]

  • 根据网络接口的索引获取名字
def print_network_interface_name():
    print(socket.if_indextoname(22))

ethernet_0

  • 根据域名获取远程主机IP
def get_remote_hostbyname():
    print(socket.gethostbyname('www.pythons.org'))

45.33.18.44

  • 根据域名获取远程主机更多信息,返回主机域名、别名列表和IP列表
def get_remote_hostbyname_ex():
    print(socket.gethostbyname_ex('www.pythons.org'))

(‘www.pythons.org’, [], [‘45.33.18.44’, ‘96.126.123.244’, ‘45.33.23.183’, ‘45.33.2.79’, ‘173.255.194.134’, ‘45.33.30.197’, ‘45.79.19.196’, ‘45.33.20.235’, ‘72.14.185.43’, ‘45.56.79.23’, ‘198.58.118.167’, ‘72.14.178.174’])

  • 获取远程主机信息
def getaddrinfo():
    info = socket.getaddrinfo("www.pythons.org", 80, proto=socket.IPPROTO_TCP)
    print(info)

[(<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘45.33.18.44’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘96.126.123.244’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘45.33.23.183’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘45.33.2.79’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘173.255.194.134’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘45.33.30.197’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘45.79.19.196’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘45.33.20.235’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘72.14.185.43’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘45.56.79.23’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘198.58.118.167’, 80)), (<AddressFamily.AF_INET: 2>, 0, 6, ‘’, (‘72.14.178.174’, 80))]

  • 参考网址:
    https://docs.python.org/3/library/socket.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1214153.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

根据视频编码时间批量重命名视频文件

整理收藏的小视频的时候发现很多视频命名很随意&#xff0c;自己命名又太麻烦&#xff0c;看着乱糟糟的文件又心烦&#xff0c;所有写了这个程序&#xff0c;代码如下&#xff1a; import osfrom filetype import filetype from pymediainfo import MediaInfovideo_extension …

【文件读取/包含】任意文件读取漏洞 afr_3

1.1漏洞描述 漏洞名称任意文件读取漏洞 afr_3漏洞类型文件读取/包含漏洞等级⭐⭐⭐⭐⭐漏洞环境docker攻击方式 1.2漏洞等级 高危 1.3影响版本 暂无 1.4漏洞复现 1.4.1.基础环境 靶场docker工具BurpSuite 1.4.2.环境搭建 1.创建docker-compose.yml文件 version: 3.2 servi…

从CentOS向KeyarchOS操作系统的wordpress应用迁移实战

文章目录 从CentOS向KeyarchOS操作系统的wordpress应用迁移实战一、使用浪潮信息X2Keyarch迁移工具完成操作系统的迁移1.1 迁移前的验证1.2 执行迁移评估1.3 开始迁移1.4 验证迁移结果1.5 迁移后的验证 二、总结 从CentOS向KeyarchOS操作系统的wordpress应用迁移实战 CentOS是一…

mysql操作 sql语句中的完整性约束有哪些,主键约束、外键约束、引用完整性约束,主键外键、唯一性

什么是约束&#xff1a;约束&#xff1a;就是约定哪些东西能填、怎么填&#xff1f;哪些东西不能填&#xff1f; 文章目录 前言&#xff1a;建表正文一、实体完整性约束1. 主键约束2. 唯一性约束3. 自增长约束4. 联合主键约束 二、域完整性约束三、引用完整性约束1. 外键约束 讲…

【Git企业开发】第七节.多人协作开发

文章目录 前言 一、多人协作开发 1.1 多人协作一 1.2 多人协作二 1.3 远程分支删除后&#xff0c;本地 git branch -a 依然能看到的解决办法 总结 前言 一、多人协作开发 1.1 多人协作一 目前&#xff0c;我们所完成的工作如下: 基本完成Git的所有本地库的相关操作&#xff0…

未来10年,NAND 与DRAM依然是存储主角

根据Yole Group调查机构的数据显示&#xff0c;在2022年独立记忆体&#xff08;Stand-alone Memory&#xff09;整体市场达到了1440亿美元。其中DRAM占比55.4%&#xff0c;NAND占比40.8%。剩下的NOR、(NV)SRAM/FRAM、EEPROM、新型非易失存储(PCM, ReRAM and STT-MRAM)等占比3.8…

蓝桥杯 string

string简介 string是C标准库的重要组成部分&#xff0c;主要用于字符串处理。 使用string库需要在头文件中包括该库 #include< string> string与char[]不同&#xff0c;string实现了高度的封装&#xff0c;可以很方便地完 成各种字符串的操作&#xff0c;比如拼接、截取…

Since Maven 3.8.1 http repositories are blocked

原因 高版本的maven不支持http的存储库。 解决方案 其实方法有好几种&#xff0c;比如降级maven版本至3.6.3(之前一直用的都是这个版本)&#xff0c;我选择了一种比较快(但不一定安全)的方式&#xff0c;因为3.6.3版本被我卸载了&#xff0c;这里直接修改idea的setting配置&…

【Java 进阶篇】JQuery 案例:下拉列表选中条目左右移动,打破选择的边界

在前端的舞台上&#xff0c;下拉列表是常见的用户交互元素&#xff0c;但有时候我们想要更多的交互体验。通过巧妙运用 JQuery&#xff0c;我们可以实现下拉列表中选中条目的左右移动功能&#xff0c;为用户提供更加灵活的选择方式。本篇博客将深入研究 JQuery 中实现这一功能的…

技巧篇:在Pycharm中配置集成Git

一、在Pycharm中配置集成Git 我们使用git需要先安装git工具&#xff0c;这里给出下载地址&#xff0c;下载后一路直接安装即可&#xff1a; https://git-for-windows.github.io/ 0. git中的一些常用词释义 Repository name&#xff1a; 仓库名称 Description(可选)&#xff1a;…

【数据结构】别跟我讲你不会冒泡排序

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前正在学习c和算法 ✈️专栏&#xff1a;数据结构 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章有啥瑕疵 希望大佬指点一二 如果文章对你有帮助…

旅拍摄影技巧澳大利亚、韩国旅行攻略

欢迎关注「苏南下」 在这里分享我的旅行和影像创作心得 刚刚在腾讯内部做了一场摄影分享课&#xff1a; 《旅拍摄影技巧&澳大利亚、韩国旅行攻略》 分享了早前去两个国家的一些旅行见闻和摄影心得。我发现&#xff1a;把自己学会的东西整理出来&#xff0c;再告诉给别人这件…

67基于matlab图像处理,包括颜色和亮度调整、翻转功能、空间滤波和去噪、频域滤波和去噪、噪声添加,形态学操作、边缘检测及示波器集成的GUI图像处理。

基于matlab图像处理&#xff0c;包括颜色和亮度调整、翻转功能、空间滤波和去噪、频域滤波和去噪、噪声添加&#xff0c;形态学操作、边缘检测及示波器集成的GUI图像处理。数据可更换自己的&#xff0c;程序已调通&#xff0c;可直接运行。 67 matlab图像处理图像降噪 (xiaohon…

cubemx工程更换同系列stm32芯片型号

cubemx工程更换stm32芯片型号 实验平台 软件&#xff1a;cubemx v6.10、vscode、MDK v5.27 旧型号芯片&#xff1a;stm32f103c8t6 新型号芯片&#xff1a;stm32f103ret6 步骤 1、复制stm32工程&#xff08;包含.ioc文件&#xff09;到另一个路径&#xff0c;新路径与原路径…

STM32F103C8T6第4天:串口实验(非中断和中断)、hc01蓝牙、esp8266WIFI、4g

1. 串口基本介绍&#xff08;332.36&#xff09; 常用函数介绍 串口发送/接收函数&#xff1a; HAL_UART_Transmit(); 串口发送数据&#xff0c;使用超时管理机制HAL_UART_Receive(); 串口接收数据&#xff0c;使用超时管理机制HAL_UART_Transmit_IT(); 串口中断模式发送HAL…

栈的三道oj【C++】

栈和队列的相关oj 最小栈思路解决代码 栈的压入弹出序列思路解决代码 逆波兰表达式思路&#xff1a;解决代码 这里就挑了三道题用来熟悉栈 最小栈 力扣链接 咱们已经是高贵的C使用者了&#xff0c;不用像C语言一样从头开始造轮子了 这里我们调用了stack后&#xff0c;就会发…

SMART PLC滑动平均值滤波(指针+FOR循环应用浮点数滤波)

SMART PLC滑动平均值滤波应用于单整型数据请查看下面文章链接: 【精选】S7-200 SMART PLC信号处理系列之滑动平均值滤波FB_smart200 滤波函数_RXXW_Dor的博客-CSDN博客文章浏览阅读1.5k次。PLC相关滤波算法,专栏有很多详细讲解这里不再赘述。滑动平均值滤波和算术平均值滤波…

halcon识别验证码,先训练后识别

识别验证码图片&#xff0c;使用halcon 21.05 下面代码识别准确率100% 目录 训练&#xff0c;图片打标签使用代码创建分类器&#xff1b;识别验证码&#xff0c;检验识别效果使用“助手”加载训练文件&#xff0c;加载训练分类器&#xff0c;察看收集的字符&#xff0c;训练识别…

创建数据透视表:根据表中一列作为分类的依据统计每个类别下不同子项数量cross_tab()

【小白从小学Python、C、Java】 【计算机等考500强证书考研】 【Python-数据分析】 创建数据透视表&#xff1a; 根据表中一列作为分类的依据 统计每个类别下不同子项数量 cross_tab() [太阳]选择题 关于以下代码的说法中正确的是? import pandas as pd data{A:[a1,a2,a1,a2,a…

StyleGAN:彻底改变生成对抗网络的艺术

一、介绍 多年来&#xff0c;人工智能领域取得了显着的进步&#xff0c;其中最令人兴奋的领域之一是生成模型的发展。这些模型旨在生成与人类创作没有区别的内容&#xff0c;例如图像和文本。其中&#xff0c;StyleGAN&#xff08;即风格生成对抗网络&#xff09;因其创建高度逼…