Skip to content

Instantly share code, notes, and snippets.

@huiliu
huiliu / ringbuffer.cpp
Last active August 20, 2017 08:37
RingBuffer.cpp
#include "stdafx.h"
#include <iostream>
#include <cassert>
#include <memory>
#include <tuple>
typedef char* CharSPtr;
class RingBuffer
{
@huiliu
huiliu / reactor.py
Created June 16, 2017 13:30
muduo python
import socket
import time
import selectors
kReadEvent = selectors.EVENT_READ
kWriteEvent = selectors.EVENT_WRITE
kNoneEvent = 0
class Poller:
""" I/O 多路分发
@huiliu
huiliu / zhihu.py
Created May 22, 2017 16:33
抓取知乎问题页面
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
class ZhihuSpider(scrapy.Spider):
name = 'zhihu'
allowed_domains = ['www.zhihu.com']
start_urls = ['https://www.zhihu.com/topic/19619463/hot']
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0",
import scrapy
class FootballTeamNormalItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
c_type = scrapy.Field() # 比赛类型
result = scrapy.Field() # 赛季战况(胜/负/平)
jingqiu = scrapy.Field() # 总进球数
shiqiu = scrapy.Field() # 失球数
@huiliu
huiliu / chat.c
Last active March 20, 2016 17:47
基于TCP的chat代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <assert.h>
@huiliu
huiliu / ping.py
Created December 30, 2015 18:03
解析nmap -sn 192.168.122.0/24 -oX ping.xml的结果
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as ET
xml_file = 'ping.xml'
def main():
host_info = list()
doc = ET.parse(xml_file)
@huiliu
huiliu / app.py
Created December 8, 2015 17:12
Flask Streaming from Templates
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, stream_with_context, request, Response, flash
from time import sleep
app = Flask(__name__)
def stream_template(template_name, **context):
@huiliu
huiliu / get_if_macaddr.c
Created October 21, 2015 07:54
取得某个网络接口的MAC地址
/*
* Demonstrates retrieving hardware address of adapter using ioctl()
*
* Author: Ben Menking <bmenking@highstream.net>
*
* 此代码源自:
* http://stackoverflow.com/questions/1519585/how-to-get-mac-address-for-an-interface-in-linux-using-a-c-program
*
*
* 编译时会提示struct ifreq的大小不知道(当前已解决)。下面有相应的讨论
@huiliu
huiliu / showif.c
Created October 21, 2015 07:47 — forked from OrangeTide/showif.c
example to list network interfaces on linux/bsd/osx/etc
/* showif.c : PUBLIC DOMAIN - Jon Mayo - August 22, 2006
* - You may remove any comments you wish, modify this code any way you wish,
* and distribute any way you wish.*/
/* finds all network interfaces and shows a little information about them.
* some operating systems list interfaces multiple times because of different
* flags, modes, etc. if you want to use this code you should be aware that
* duplicate interfaces is a possibility */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@huiliu
huiliu / getnifs.py
Created October 15, 2015 06:39 — forked from provegard/getnifs.py
Python script for listing network interfaces with name, index and addresses
#!/usr/bin/python
# Based on getifaddrs.py from pydlnadms [http://code.google.com/p/pydlnadms/].
# Only tested on Linux!
from socket import AF_INET, AF_INET6, inet_ntop
from ctypes import (
Structure, Union, POINTER,
pointer, get_errno, cast,
c_ushort, c_byte, c_void_p, c_char_p, c_uint, c_int, c_uint16, c_uint32