[python&php 网络编程]格式化IPv4地址

IP 协议报文格式 【IPv4】 前言: IP 数据报文由首部(称为报头)和数据两部分组成;首部的前一部分是固定长度,共 20 字节,是所有 IP 数据报必须具有的;在首部的固定部分的后面是一些可选字段,其长度是可变的 每个 IP 数据报都以一个 IP 报头开始;源计算机构造这个 IP 报头,而目的计算机利用 IP 报头中封装的信息处理数据;IP 报头中包含大量的信息,如源 IP 地址、目的 IP 地址、数据报长度、IP 版本号等;每个信息都被称为一个字段 IP 协议报文格式版本号首部长度区分服务 TOS总长度 如图所示: 版本号 占 4 阅读详情

1.python写法

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()

2.php写法

<?php

function convert_ip4_address(){
    foreach(array('127.0.0.1', '192.168.0.1') as $ip_addr){
        $packed_ip_addr = ip2long($ip_addr);
        $unpacked_ip_addr = long2ip($packed_ip_addr);
        printf("IP Address: %s => Packed: %s, Unpacked: %s\n", $ip_addr, bin2hex(pack('N', $packed_ip_addr)), $unpacked_ip_addr);
    }
}

convert_ip4_address();

inet_aton()是一个改进的方法来将一个字符串IP地址转换为一个32位的网络序列IP地址。

inet_ntoa()是将一个IP转换成一个互联网标准点分格式的字符串。

但这两个函数php都没有对应的现成函数,我只好这么干了,可以看到做法慢慢复杂了,性能自然没法和python比~

转载请标注原文地址:http://blog.youkuaiyun.com/ddjohn/article/details/49930573

机器学习模型系列:随机森林的原理和示例介绍 让我们总结一下与其他监督式机器学习模型相比,随机森林的优缺点。优点在许多真实数据集上已知能提供高度准确的模型。通过结合多个决策树的预测,可以捕捉数据集中的复杂交互和模式。通过自动选择相关特征,可以有效处理高维数据集。与单个决策树相比,不容易过拟合。自助采样和每个节点的随机特征选择有助于减少过拟合并改善泛化能力。可以处理包括数值和分类特征在内的异构数据类型。可以处理缺失值而不需要填充。提供特征重要性的度量。集成中的树可以并行训练,因为每个决策树可以独立构建。模型的泛化误差具有理论上的界限。 阅读详情

相关推荐

雷达原理习题答案【西电】

  目前教材用的是陈老师新版的,但是,我发现课后题有一部分是之前某本书里的。答案如下【建议参看原题→查阅答案】 【原题点这里!!点这里!!】 目录跳转功能目前好像只支持网页版 文章目录% 第一章、第二章% 第三章% 第四章% 第五章% 第六章% 第七章% 第八章 % 第一章、第二章 % 第三章 % 第四章 % 第五章 % 第六章 % 第七章 % 第八章 ...

薛定谔的壳的博客 1万+

IP数据报格式详解

使用IP在互联网上传输的数据是在被称为IP数据报的消息中携带的. 就像所有的网络协议消息一样,IP对它的数据报使用一种特定的格式。这里我们使用的是IPv4,所以我们将研究IPv4的数据报格式。 IPv4数据报在概念上分为两部分:header和payload。头部包含地址和控制字段。而payload携带在网络上发送的实际数据。 尽管IP是相对简单的,无连接的不可靠协议。IPv4报头包含了相当多的信息,这使得它相当的大。至少它是20个字节长,并且携带其他选项的话会更长。IP数据包格式将在表Table 1和图

humanhaunt的博客 2027

Cadence创建异形焊盘教程(详细操作)

背景:一些常见的矩形/圆形焊盘,设置参数就可。碰到非规则的一些焊盘,那只能用Cadence Allegro PCB来画咯。 一、新建图形 【File】——>【New…】 弹出如下窗口,选择绘制的类型Shape symbol、路径,输入名字 二、设置参数 【Setup】——>【Design Parameters…】 Enhanced display modes:增强型显示模型。全部勾选上。 在【Design】设置单位为mm 设置偏移量(-50,-50) 三、绘制图形 1、找到原点,即参考

qq_42057393的博客 2万+

MicroPython标准库

MicroPython标准库使用参考示例

若有博文对您有帮助,欢迎点赞关注,收藏评论。 2335

vscode+keil嵌入式软件开发全流程

vscode+keil嵌入式软件开发全流程

AspringQ的博客 6784

零基础教你学RDMA驱动开发--第8篇:队列对(QP)、完成队列(CQ)与工作请求(WR)机制剖析

本文深入剖析了RDMA(远程直接内存访问)中的核心机制:队列对(QP)、完成队列(CQ)和工作请求(WR)。QP作为通信端点包含发送队列(SQ)和接收队列(RQ);WR是用户提交的操作指令,转换为工作队列元素(WQE)后由网卡异步执行;CQ则提供完成通知机制。文章详细介绍了三者的协作原理,包括QP状态机、WR提交方式、CQ轮询机制,以及关键的信号控制技术(Signaled Mode)来优化性能。同时对比了SEND/RECV和RDMA WRITE两种模式的差异,提供了调试技巧和最佳实践。这些机制共同构成了RD

amy_mhd的博客 157

Python格式化字符串和随机生成ip地址

记录一下。 随机生成IPv4地址: import random import socket import struct return socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff))) 随机生成IPv6地址: ':'.join('{:x}'.format(random.randint(0, 2**16 - 1

changzhi的专栏 7820

利用Python爬虫采集京东商品评论情感分析案例(含完整代码)

在电商蓬勃发展的今天,商品评论蕴含着巨大的商业价值。本文以京东商品评论为研究对象,系统介绍如何使用Python构建一个完整的评论采集与情感分析系统。文章涵盖京东反爬机制分析、动态请求模拟、评论数据解析、中文文本预处理、基于SnowNLP和深度学习的情感极性分类、以及可视化展示等完整技术链路。所有代码均基于2025-2026年最新可用接口编写,并提供可直接运行的完整源码。全文力求详实,总字数逾八千字,适合Python数据分析从业者、电商运营人员及自然语言处理初学者参考实践。目录摘要第一章 引言。

2201_76125261的博客 88

python中处理IP地址

IPy - 提供了一些有用的类和函数来处理ipv6和ipv4地址和网络 项目地址: https://github.com/haypo/python-ipy/ API ======================= IPy库包含类IP,使用它你可以方便处理对大部分格式的ipv...

529

ESP32-IDF开发之旅 外设篇④ 串口通信 Uart

ESP32-S3 UART串口通信详解 本文介绍了ESP32-S3的UART串口通信配置流程,涵盖硬件引脚分配、参数设置、数据收发等关键步骤。

单片机菜鸟哥的博客 495

ATT MTU 交换机制:提升 BLE 数据传输效率的关键

在低功耗蓝牙(BLE)技术的面试中,ATT MTU 交换机制始终是高频考点。无论是芯片厂商的固件开发岗,还是物联网设备的协议栈调试岗,面试官都爱通过这个知识点考察候选人对 BLE 数据传输底层逻辑的理解。本文将系统梳理核心考点,结合历年真题详解应答思路,轻松应对这类问题。​

byte轻骑兵的技术小窝 2411

python ip地址处理_python中处理IP地址

python中处理IP地址IPy - 提供了一些有用的类和函数来处理ipv6和ipv4地址和网络APIIPy库包含类IP,使用它你可以方便处理对大部分格式的ipv6和ipv4网络和地址的格式化和处理。它的接口借鉴了Perl的NET::IP模块,但是实现上采用了不同的方法。它没有实现non-CIDR网络掩码,所以打算使用0xffffff0f格式网络掩码的无法使用。>>> from ...

weixin_31255215的博客 2247

Yandex复杂还原验证码识别

本文分析了Yandex滑动验证码的识别方法,指出其通过JS判断正确滑动位置而非用户交互还原图像。核心思路是通过像素差异分析,将验证码图像重组后(分为3×3或4×4),计算相邻图像间的像素差异,最小值即为正确位置。与前期某象验证码识别方法类似,需结合图像增强技术统一像素直方图。文章强调该方法仅供学习参考,严禁用于违法行为。完整识别流程包括图像重组、像素差异计算等步骤,最终可准确定位正确拼图位置。

码王吴彦祖的博客 285

python IP格式校验

校验对于web开发同学来说,写代码时是必不可少的。     不管前端有没有做校验,后端还是必须得要做校验。出于安全性考虑,可以有效的防止sql注入。     校验其实是一件很无聊的事情,重复写着代码。所以整理出通用的接口还是蛮有必要的。今天贴出一版python的IP校验接口,包括IPv4和IPv6的校验 IPv4: def validate_ip(ip_str): sep

zhubaoJay的博客 6070

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

网络基础(一)

网络基础

weixin_54428357的博客 203

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

使用Python获取并处理IP的类型及格式

公网与私有网络的判断其实十分简单,只要记住私有网络的三个网段。不过,对于记性不好的人或者学识不是很高的机器来说,有一种判断方法还是有必要的。 写如下脚本:   1 from IPy import IP   2    3 ip1 = IP('192.168.1.2')   4 ip2 = IP('11.12.13.14')   5    6 print("ip1 type: %s"

小灰笔记 3213

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

网络原理-TCP/IP

TCP 和 UDP 没有绝对的优劣之分。它们是程序员可以根据具体应用场景和需求选择的工具。需要可靠性时选 TCP,需要速度和实时性或广播时选 UDP。

2403_83543687的博客 1946

Micropython——network配置和使用

1、network是什么?此模块提供网络驱动程序和路由配置。要使用此模块,必须安装具有网络功能的MicroPython变体/构建。此模块中提供了特定硬件的网络驱动程序,用于配置硬件网络接口。然后,配置接口提供的网络服务可通过usocket模块使用。2、network方法和类使用? 4、示例......

Xa_L 3982

java格式化mac地址,java格式化mac地址(加分隔符,去分隔符)

项目中MAC地址经常需要格式化,有的需要分隔符,有的不需要分隔符,比较烦。自己实现了这两个方法,大佬轻喷1、类似12ae5bac34c4中间无分隔符,需要在中间加入分隔符(:或者-),最终效果:12:ae:5b:ac:34:c4方法:public static String formatMac(String mac, String split) {String regex = "[0-9a-fA-...

weixin_28759537的博客 877

Python 将 IPv4 地址转换成不同的格式

Python 将 IPv4 地址转换成不同的格式

super_technology的博客 1004

格式化输出IP地址

public static String getIp(Context ctx) { return Formatter.formatIpAddress((WifiManager) ctx.getSystemService(Context.WIFI_SERVICE).getConnectionInfo().getIpAddress()); }

xiyangyang8110的博客 2388

json在线格式化地址

https://www.json.cn/ 转载于:https://www.cnblogs.com/feifeicui/p/9337288.html

weixin_30632899的博客 335

Python3---binascii模块和进制转换笔记

废话少说,直接上代码: # !/usr/bin/env python # -*- coding:utf-8 -*- # author:大西瓜 # 导入binascii模块 import binascii a = b'BE27E8FFFF010203' # 先把b'BE27E8FFFF010203'转换成二进制数据然后在用十六进制表示 b = binascii.b2a_hex(a) # 打印...

大西瓜Blog 9353

格式化带中文地址

package test; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; /** * 格式化带中文地址(http://) * @author 浴缸 * */ public class FormatUrl { public static String formatUrl(Strin...

yu_kang的博客 691

android eth0 ipv4,android - How do I get IP_ADDRESS in IPV4 format - Stack Overflow

After trying many tricks.. finally I can get the IP address in IPV4 format.. Here is my code..public String getLocalIpAddress() {try {for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en....

weixin_31556767的博客 165
上一篇: [python&php 网络编程]获取远程机器的信息
下一篇: [python&php 网络编程]通过端口号获取服务名
ddjohn
博客等级 码龄17年 36粉丝 135原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值