Skip to content

Instantly share code, notes, and snippets.

@vermaslal
vermaslal / dummy-web-server.py
Last active September 29, 2015 06:07 — forked from bradmontgomery/dummy-web-server.py
a minimal http server in python. Responds to GET, HEAD, POST requests, but will fail on anything else.
#!/usr/bin/env python
"""
Very simple HTTP server in python.
Usage::
./dummy-web-server.py [<port>]
Send a GET request::
curl http://localhost
@vermaslal
vermaslal / http_req_response.py
Created October 3, 2015 10:46
Python code to create a HTTP post request and handle json request
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import simplejson
import random
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
@vermaslal
vermaslal / virtualStream
Created October 16, 2015 12:19
virtualStream in node js
var fs = require('fs');
var stream = require('stream');
var util = require('util');
function virtualStream() {
stream.Transform.call(this);
this._readableState.objectMode = false;
this._writableState.objectMode = true;
}
@vermaslal
vermaslal / multithread.py
Created October 17, 2015 10:59
Multi threaded python light web server
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
message = threading.currentThread().getName()
@vermaslal
vermaslal / auth.php
Created October 28, 2015 05:56
Prompt Browser password dialog : Browser Password Dialog « Login Authentication « PHP
<?php
if (!isset($_SERVER['PHP_AUTH_USER']) || ($_SERVER['PHP_AUTH_USER'] != 'specialuser') || ($_SERVER['PHP_AUTH_PW'] != 'secretpassword')) {
header('WWW-Authenticate: Basic Realm="Secret Stash"');
header('HTTP/1.0 401 Unauthorized');
print('You must provide the proper credentials!');
exit;
} else {
print('Authenticated!');
}
@vermaslal
vermaslal / ksort.js
Created October 30, 2015 12:39 — forked from stiekel/ksort.js
JavaScript ksort
function ksort(obj){
var keys = Object.keys(obj).sort()
, sortedObj = {};
for(var i in keys) {
sortedObj[keys[i]] = obj[keys[i]];
}
return sortedObj;
}
@vermaslal
vermaslal / node_cluster.js
Last active October 31, 2015 12:09
Improve node.js app performance using Clustering
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var express = require("express");
var app = express();
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
@vermaslal
vermaslal / replace_href_anchor.js
Created November 25, 2015 11:32
Replace href from anchor tag html using node js
var source = '<a href="double-quote test">double-quote test</a>\n' +
'<a href=\'single-quote test\'>single-quote test</a>\n' +
'<a href=\'single-quote test\'>single-quote test</a>\n' +
'<a class="foo" href="leading prop test">\n' +
'<a href="trailing prop test" class="foo">\n' +
'<a style="bar" link="baz" ' +
'name="quux" ' +
'href="multiple prop test" class="foo">\n' +
'<a class="foo"\n href="inline newline test"\n style="bar"\n >inline newline test</a>' +
'<a href=3D"with 3d data double quote"\n style="bar"\n >with 3d data double quote</a>' +
@vermaslal
vermaslal / streamMiddle.js
Last active December 8, 2015 10:09
To update stream on middle using node js
'use strict';
//Reference: https://www.safaribooksonline.com/blog/2013/05/01/using-streams-in-node-js/
var fs = require('fs');
var stream = require('stream');
//var Transform = require('stream').Transform;
var mx = {};
mx.bind_helo = '--------sham-----';
var source = fs.createReadStream('/home/sarv/node_modules/smail/s.txt');
@vermaslal
vermaslal / queryStringParam.js
Created December 14, 2015 09:36
jQuery Get Query String Parameters as object
$.extend({
getUrlVars: function () {
var vars = {}, hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
return vars;
},