Skip to content

Instantly share code, notes, and snippets.

View arseniyturin's full-sized avatar
🏠
Working from home

Arseny Turin arseniyturin

🏠
Working from home
View GitHub Profile
import socket
from threading import Thread
def server(host='', port=8000):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
sock.bind((host, port))
sock.listen()
while True:
client, address = sock.accept()
@arseniyturin
arseniyturin / asyncio-socket-server.py
Created December 9, 2021 19:56
Example of the asynchronous socket server with asyncio
# David Beazley async socket server example
# Loops very similar to a 'regular' threaded socket server
# Except we create an event loop and use async/await syntax
import asyncio
import socket
loop = asyncio.get_event_loop()
async def server(address):
'''
Basic Python Threaded TCP Server
--------------------------------
socketserver.TCPServer requires only two things:
- Address (host, port)
- Handler class to process requests
Must be derived from `BaseRequestHandler` or `StreamRequestHandler`
'''
from socketserver import TCPServer
def handshake(frame, client):
headers = http_message.decode('utf8').split('\n')
for header in headers:
if header.startswith('Sec-WebSocket-Key'):
key = header.replace('Sec-WebSocket-Key:', '').strip()
accept_key = generate_accept_key(key)
response = (
"HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
from hashlib import sha1
from base64 import b64encode
def generate_accept_key(key):
GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
sha1_hash = sha1((key + GUID).encode()).digest()
return b64encode(sha1_hash).decode()
# simplest
import socket
from threading import Thread
def response(client, address):
print(f'Client connected from: {address}')
while True:
# receiving message
message = client.recv(1024)
if not message:
'''
100 coins / 99 fair + 1 unfair (both heads)
pick one
flip 10 times = 10 heads
what is the probability that this coin is unfair?
P(A) * P(B|A)
P(A|B) = ----------------
P(B)
@arseniyturin
arseniyturin / rain_bayes.py
Last active July 30, 2021 16:37
Should you bring an umbrella if 3 of your friends said it will rain?
'''
Question: You're travelling to Seattle and you've asked 3 of your friends independently if it will rain,
3 of them said it will. Each friend has 2/3 chances of being right. Should you bring an umbrella?
Assume chance of rain is 25%
P(rain) * P(yes | rain)
P(rain | yes) = ------------------------
P(yes)
@arseniyturin
arseniyturin / cookie_bayes.py
Created July 30, 2021 15:57
Naive Bayes Cookie Problem
@arseniyturin
arseniyturin / sqlite_top_n.py
Last active June 12, 2021 17:25
SQLite: select top n products from each category
"""
Table 'Products' contains different products, their categories and prices.
We want to select top n products per each category rated by their price
SOLUTION: Window function RANK()
1. TABLE 'Products'
┌───────────┬────────────┬───────┐
│ product │ category │ price │
├───────────┼────────────┼───────┤
│ product_A │ category_A │ 55 │