Skip to content

Instantly share code, notes, and snippets.

@etng
etng / pyftpd.py
Created September 23, 2014 08:27
ftp daemon implement in python using pyftpdlib
# pip install pyftpdlib
# http://pythonhosted.org/pyftpdlib/tutorial.html
import logging
import os
try:
from hashlib import md5
except ImportError:
from md5 import new as md5 # Python < 2.5
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler,ThrottledDTPHandler
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.conf import settings
from django.utils.encoding import force_unicode
import redis
class SessionStore(SessionBase):
""" Redis store for sessions"""
def __init__(self, session_key=None):
self.redis = redis.Redis(
@etng
etng / makeHtmlExcerpt.php
Created September 11, 2014 22:21
make exerpt from html and auto close tags,break at the first br or closing block tag
<?php
//will auto close tags at the first real line break.
//makeExerpt('<p>line 1 <strong>he<br/>llo</strong>
//<br/>line 1 breaked
//<div>line 2
//</div>line3
//</p>
//');
//produce <p>line 1 <strong>he</strong></p>
@etng
etng / protect_rm
Created August 6, 2014 01:41 — forked from hanks/protect_rm
# replace rm command with mv to Trash to protect remove files or directory miss
function __protect_rm {
mv ${@: -1} ~/.Trash #get last function argument
}
alias rm='__protect_rm'
#!/usr/bin/env python
"""
@author Sunil Mallya
Sample code to show a parent - child like process communication model where parent listens on a port and passes the pickled file descriptor
to the child process to read the bytes off the socket. The communication in this snippet is via a Queue which is thread/process safe
Just to be clear, the parent process is still accepting the connection and we are sending a live fd to the child
"""
import os
import sys
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid

Intercepts HTTPs Traffic with Python & mitmproxy

Introduction

Modern applications usually make use of back-end API servers to provide their services. With a non-transparent HTTPs proxy, which intercepts the communication between clients and servers (aka the man-in-the-middle scheme), you can easily manipulate both API requests and responses.

This manual helps you create your own proxy with Python and mitmproxy/libmproxy. Mitmproxy ships with both a standalone command-line tool (mitmproxy) and a Python library (libmproxy).

from django.db import connections
from django.db.models.query import QuerySet
class ApproxCountQuerySet(QuerySet):
"""Counting all rows is very expensive on large Innodb tables. This
is a replacement for QuerySet that returns an approximation if count()
is called with no additional constraints. In all other cases it should
behave exactly as QuerySet.
Only works with MySQL. Behaves normally for all other engines.
import logging
import subprocess
import time
import sys
import win32con
import win32gui_struct
import os
try:
import winxpgui as win32gui
except ImportError:
class BridgeCallback implements IOAcknowledge, IOCallback {
...
@Override
public void ack(Object... args) {
String argsStr = TextUtils.join(",", args);
final String callback = "javascript:" + mCallback + "(" + argsStr + ");";
((MainActivity)mContext).runOnUiThread(new Runnable() {
public void run() {
mWebView.loadUrl(callback);