Skip to content

Instantly share code, notes, and snippets.

View gsw945's full-sized avatar
🙏
buddha-like coding

玖亖伍 gsw945

🙏
buddha-like coding
View GitHub Profile
@gsw945
gsw945 / cat_line.py
Created July 19, 2017 10:04
show file content like Linux `cat` command but only show specified line
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import os
import linecache
import sys
#!/usr/bin/env python
# A simple demonstration of using cairo to shape windows.
# Natan 'whatah' Zohar
import gtk
import math
class ShapedGUI:
def __init__(self):
self.window = gtk.Window()
self.window.show() # We show here so the window gets a border on it by the WM

Python Socket 编程详细介绍

Python 提供了两个基本的 socket 模块:

  • Socket 它提供了标准的BSD Socket API。
  • SocketServer 它提供了服务器重心,可以简化网络服务器的开发。

下面讲解下 Socket模块功能。

Socket 类型

# -*- coding: utf-8 -*-
import hashlib
def file_md5sum(file_path):
'''计算文件md5值'''
hash_md5 = hashlib.md5()
chunk_size = 4096
with open(file_path, "rb") as f:
chunk = f.read(chunk_size)
while bool(chunk):
# -*- coding: utf-8 -*-
import os
def print_size(size):
'''打印文件大小'''
print('Size: {0} Byte'.format(size))
print('Size: {0} KB'.format(size / 1024))
print('Size: {0} MB'.format(size / 1024 / 1024))
print('Size: {0} GB'.format(size / 1024 / 1024 / 1024))
@gsw945
gsw945 / top-win.cs
Created June 20, 2018 06:16
single cs file for winfrom by csc.exe from cli
using System; // Console
using System.Runtime.InteropServices; // DllImport、StructLayout
using System.Windows.Forms; // Screen、Form
using System.Drawing; // Rectangle
using System.ComponentModel;
using System.Diagnostics; // Process
/**
* https://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-build-a-single-file-assembly
* https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe
@gsw945
gsw945 / logo.py
Created August 2, 2018 13:53 — forked from SuperDoxin/logo.py
Python Logo using bezier curves and the turtle module
import turtle
import math
def lerp(a, b, t):
"""Linear interpolation function. returns a when t==0, returns b when t==1
and linearly interpolates for values inbetween"""
return (a * (1 - t)) + (b * t)
@gsw945
gsw945 / flask_reverse_proxy2other_server.py
Created August 11, 2018 10:37
flask reverse proxy to other server
import logging
try:
from urllib.parse import (
urlencode,
parse_qs,
urlsplit,
urlunsplit
)
except ImportError:
from urllib import urlencode
@gsw945
gsw945 / simple-ddos.py
Created August 12, 2018 07:08
simple ddos via multiprocessing(.dummy) and requests
# -*- coding: utf-8 -*-
import os
import sys
import multiprocessing as mp
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
from datetime import datetime
try:
import requests
except ImportError:
@gsw945
gsw945 / static-server.py
Last active August 12, 2018 07:24
static web server via aiohttp
# -*- coding: utf-8 -*-
# need to upgrade the default Python version on Ubuntu 16.04
# refer:
# https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get
# https://aiohttp.readthedocs.io/en/stable/web_advanced.html#static-file-handling
import os
import logging
try:
from aiohttp import web