Skip to content

Instantly share code, notes, and snippets.

View Bogdanp's full-sized avatar

Bogdan Popa Bogdanp

View GitHub Profile
@Bogdanp
Bogdanp / lol.py
Created December 27, 2015 12:22
lol Python
import ast
import inspect
class NameVisitor(ast.NodeVisitor):
def __init__(self, default_value=None):
self.default_value = default_value
def visit_Name(self, node):
if isinstance(node.ctx, ast.Load):
module Main exposing (main)
import Html exposing (div)
import Navigation
x =
42
enum Status {
Todo,
Complete,
Deleted
}
record Unit {}
record Todo {
id Int
deadline DateTime
### Keybase proof
I hereby claim:
* I am Bogdanp on github.
* I am bogdanp (https://keybase.io/bogdanp) on keybase.
* I have a public key whose fingerprint is E05E B778 DDF1 30C5 22C6 3B03 F3FF 10E0 16B3 1890
To claim this, I am signing this object:
import socket
HOST = "127.0.0.1"
PORT = 9000
# By default, socket.socket creates TCP sockets.
with socket.socket() as server_sock:
# This tells the kernel to reuse sockets that are in `TIME_WAIT` state.
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
with socket.socket() as server_sock:
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_sock.bind((HOST, PORT))
server_sock.listen(0)
print(f"Listening on {HOST}:{PORT}...")
client_sock, client_addr = server_sock.accept()
print(f"New connection from {client_addr}.")
RESPONSE = b"""\
HTTP/1.1 200 OK
Content-type: text/html
Content-length: 15
<h1>Hello!</h1>""".replace(b"\n", b"\r\n")
with socket.socket() as server_sock:
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_sock.bind((HOST, PORT))
RESPONSE = b"""\
HTTP/1.1 200 OK
Content-type: text/html
Content-length: 15
<h1>Hello!</h1>""".replace(b"\n", b"\r\n")
with socket.socket() as server_sock:
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_sock.bind((HOST, PORT))
import typing
def iter_lines(sock: socket.socket, bufsize: int = 16_384) -> typing.Generator[bytes, None, bytes]:
"""Given a socket, read all the individual CRLF-separated lines
and yield each one until an empty one is found. Returns the
remainder after the empty line.
"""
buff = b""
while True:
with socket.socket() as server_sock:
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_sock.bind((HOST, PORT))
server_sock.listen(0)
print(f"Listening on {HOST}:{PORT}...")
while True:
client_sock, client_addr = server_sock.accept()
print(f"New connection from {client_addr}.")
with client_sock: