Skip to content

Instantly share code, notes, and snippets.

View yingshaoxo's full-sized avatar
🧠
Work in a health way.

yingshaoxo yingshaoxo

🧠
Work in a health way.
View GitHub Profile
@yingshaoxo
yingshaoxo / asyncore_server.py
Last active May 28, 2017 09:44
asyncore TCP server
import asyncore
connected_sockets = dict()
class SocketHandler(asyncore.dispatcher_with_send):
def handle_read(self):
data = self.recv(8192)
#print(connected_sockets)
if data:
for i in [i for i in connected_sockets.values() if i != self.socket]:
@yingshaoxo
yingshaoxo / asyncore_client.py
Last active May 28, 2017 09:43
asyncore TCP client
import asyncore
import threading
class MySocketClient(asyncore.dispatcher):
def __init__(self, server_address):
asyncore.dispatcher.__init__(self)
self.create_socket()
self.connect(server_address)
def handle_read(self):
@yingshaoxo
yingshaoxo / asyncio_server.py
Last active July 8, 2020 08:05
asyncio TCP server
import asyncio
class EchoServerClientProtocol(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
self.peername = transport.get_extra_info('peername')
print('Connection from {}'.format(self.peername))
def data_received(self, data):
@yingshaoxo
yingshaoxo / asyncio_client.py
Last active May 29, 2017 23:37
asyncio TCP client
import asyncio
import threading
class EchoClientProtocol(asyncio.Protocol):
def __init__(self, message, loop):
self.message = message
self.loop = loop
def connection_made(self, transport):
transport.write(self.message.encode())
@yingshaoxo
yingshaoxo / asyncio_unbreakable_server.py
Last active July 22, 2017 06:56
asyncio unbreakable server, support auto reconnection.
import asyncio
import threading
import time
connected_transport = dict()
class ServerClientProtocol(asyncio.Protocol):
@yingshaoxo
yingshaoxo / asyncio_unbreakable_client.py
Last active July 22, 2017 06:58
asyncio unbreakable client, support auto reconnection.
import asyncio
import threading
import time
from datetime import datetime
SERVER_ADDRESS = '127.0.0.1' # server address
PORT = 8888
@yingshaoxo
yingshaoxo / sqlalchemy_example.py
Created November 20, 2017 17:25
This is an example shows how to use sqlalchemy.
# https://docs.sqlalchemy.org/en/latest/orm/tutorial.html
# https://bytefish.de/blog/first_steps_with_sqlalchemy/
from pprint import pprint
import os
if os.path.exists('userdata.sqlite'):
os.remove('userdata.sqlite') # not necessarily, just for testing
from sqlalchemy import create_engine
@yingshaoxo
yingshaoxo / pylab_draw_tangent_line.py
Created November 30, 2017 14:58
pylab draw tangent lines
import numpy as np
import pylab
class MathDraw():
def __init__(self, func):
self.f = func
self.vf = np.vectorize(func)
def draw(self, x_start=-5.0, y_end=5.0):
x = np.arange(x_start, y_end, 0.1)
@yingshaoxo
yingshaoxo / keep program running in linux.md
Last active January 6, 2018 15:12
Keep python script running by systemd service (how to keep program running in linux)

0. write a python script

vim time_logger.py

import os
import logging
import time

current_dir = os.path.abspath(os.path.dirname(__file__))
@yingshaoxo
yingshaoxo / DNS tunnel.md
Last active February 12, 2023 03:52
How to Set DNS tunnel