This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 │ |
NewerOlder