Skip to content

Instantly share code, notes, and snippets.

View hiway's full-sized avatar

Harshad Sharma hiway

View GitHub Profile
@hiway
hiway / zmq_http_server_example.py
Last active July 11, 2020 06:53 — forked from malexer/zmq_http_server_example.py
Hello World HTTP server in pyzmq (using ZeroMQ RAW socket) - a Python3.5+ version of http://gist.github.com/hintjens/5480625
# coding=utf-8
import zmq
DEFAULT_PAGE = '\r\n'.join([
"HTTP/1.0 200 OK",
"Content-Type: text/plain",
"",
"Hello, World!",
])

Keybase proof

I hereby claim:

  • I am hiway on github.
  • I am hiway (https://keybase.io/hiway) on keybase.
  • I have a public key ASC_73ehlJyh8_3MtMl1vFFdK_dayNqE095CWeDuiAHLgAo

To claim this, I am signing this object:

from zentropi import Agent
from zentropi import on_timer
from zentropi import on_message
class Leonidas(Agent):
@on_timer(3)
def ping(self):
self.message('Spartans?')
@hiway
hiway / micropython-python-coroutine.py
Created February 28, 2017 21:40
Boilerplate code for iscoroutinefunction() to detect `async def`s
# $ micropython -m upip install micropython-inspect
try: # python 3.5+
from inspect import iscoroutinefunction
import asyncio
except ImportError: # micropython 1.8+
from inspect import isgeneratorfunction
import uasyncio as asyncio
def iscoroutinefunction(func):
return isgeneratorfunction(func) or repr(func).startswith('<closure <generator')
@hiway
hiway / scriptssh.py
Last active January 31, 2017 06:13
A quick hack to explore ideas for scripting automated ssh interactions in Python3.5 (Example at bottom creates and bootstraps a FreeBSD jail.)
import asyncio
import logging
import os
import threading
from typing import List
import asyncssh
import janus
LOG_LEVEL = logging.DEBUG
#!/usr/bin/env python
import asyncio
from prompt_toolkit import CommandLineInterface
from prompt_toolkit.shortcuts import create_prompt_application
from prompt_toolkit.shortcuts import create_asyncio_eventloop
def get_cli(event_loop):
return CommandLineInterface(application=create_prompt_application(),
@hiway
hiway / readline_autocomplete_file_names.py
Created January 25, 2017 00:57
Seems to work, with rough edges.
import os
import readline
AUTOCOMPLETE_CACHE = {}
def completer(text, state):
global AUTOCOMPLETE_CACHE
buffer = os.path.expanduser(readline.get_line_buffer())
cached_response = AUTOCOMPLETE_CACHE.get(buffer, [])
@hiway
hiway / django_management_command.py
Created January 22, 2017 12:55
Boilerplate code for django management command.
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = ''
def handle(self, *args, **options):
try:
pass
except Exception:
@hiway
hiway / itunes_http_api.py
Created January 18, 2017 12:23
Quick n' dirty HTTP API to play/pause and cue a playlist on iTunes in MacOS.
import os
from flask import Flask, request
app = Flask(__name__)
def osascript_multiline_compress(script):
script = ["-e '{line}'".format(line=line) for line in script.split('\n')]
return 'osascript ' + ' '.join(script)
import usocket as socket
import ussl as ssl
def http_get(url):
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 443)[0][-1]
s = socket.socket()
s.connect(addr)
s = ssl.wrap_socket(s)
s.write(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))