12/23
2017

python requests get remote IP Address

引子

写爬虫的时候有个需求,获取到远端服务器的IP地址和端口。So easy,谷歌走起,于是得到了以下方式:

No.1

rsp = requests.get(..., stream=True)
rsp.raw._connection.sock.getpeername()

需要说明的是,我亲测了一下requests的2.14.2和2.18.4两个版本的python2和python3版本,其中,connection_connection其实是一个对象实例。

>>> rsp.raw.connection == rsp.raw._connection
True
11/23
2017

【go程序】bytes.Buffer在http客户端中的坑

引子

将bytes.Buffer中的内容通过http发送出去,然后查看bytes.Buffer中的内容。代码如下:

11/03
2017

探讨IP首部校验码为什么使用反码求和

IP首部校验码算法

IP首部校验和字段是根据IP首部计算的检验和码。它不对首部后面的数据进行计算。

注:IP首部一般有20个字节,如果有选项字段的话,会占用更多字节。占用字节数可以 通过首部的第4-7bit位的值获得,4bit位的值最大为16,而它的单位是32bit位,32bit 位占4个字节,所以IP首部最多为16X4=64个字节。

10/16
2017

矩阵

顺时针旋转矩阵90度

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

09/23
2017

排序

冒泡排序

基本思想

在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

示例

初始状态:2, 3, 2, 5, 1
2 < 3, pass
3 > 2, 交换位置,变为2, 2, 3, 5, 1
3 < 5, pass
5 > 1, 交换位置,变为2, 2, 3, 1, 5
09/19
2017

对有序数组去重

看题目:

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

简单翻译一下,输入一个有序数组,将数组去重,使得每个元素在数组中只会出现一次。输出是去重后的数组长度。

限制是不能使用额外的数组。即使数组在返回的长度之后还有元素也没有关系。

09/18
2017

使用rename和匹配模式批量修改文件名

关于rename的用法能查到很多,包括修改后缀名,删除后缀,添加后缀,添加前缀之类的。不再赘述。

今天遇到一个情况:批量修改图片的名字。图片的名字格式如下:

37_Why.jpg  38_Skype.jpg  488_abc.jpg

想要把他们批量修改为:

37.png  38.png  488.png
09/06
2017

计算两个数组的中位数

Question

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

前提条件

两个数组已排序

Contents

09/05
2017

ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
08/31
2017

一直在用flask写一些小型的后台服务。有余力之下,去研究了一下flask的源码。不得不赞叹flask对于python的运用之炉火纯青。下面开始分析。

flask框架使用了库werkzeug,werkzeug是基于WSGI的,WSGI是什么呢?(Web Server Gateway Interface),WSGI是一个协议,通俗翻译Web服务器网关接口,该协议把接收自客户端的所有请求都转交给这个对象处理。 基于如上背景知识,这里不只是研究了flask的源码,也会把涉及到的werkzeug库的源码,以及werkzeug库使用到的python基本库的源码列出来,力求能够把整个逻辑给陈列清楚。

本站总访问量 本站访客数人次 本文总阅读量