Skip to content

Commit 9526c9d

Browse files
committed
other
1 parent 54d10ee commit 9526c9d

File tree

18 files changed

+339
-17
lines changed

18 files changed

+339
-17
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

socket/basic/version-4/client.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# https://docs.python.org/3.5/library/socket.html
5+
#
6+
7+
import socket
8+
import time
9+
10+
# --- constants ---
11+
12+
HOST = '' # (local or external) address IP of remote server
13+
PORT = 8000 # (local or external) port of remote server
14+
15+
# server can have local address IP - used only in local network
16+
# or external address IP - used in internet on external router
17+
# (and router redirects data to internal address IP)
18+
19+
# --- create socket ---
20+
21+
print('[DEBUG] create socket')
22+
23+
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
24+
s = socket.socket() # default value is (socket.AF_INET, socket.SOCK_STREAM)
25+
# so you don't have to use it in socket()
26+
27+
# --- connect to server ---
28+
29+
print('[DEBUG] connect:', HOST, PORT)
30+
31+
s.connect((HOST, PORT)) # one tuple (HOST, PORT), not two arguments
32+
33+
# --- sending receivng many times ---
34+
35+
try:
36+
while True:
37+
38+
now = int(time.time())
39+
40+
# --- send data ---
41+
42+
# if you don't use native characters
43+
# then you can use 'ascii' instead of 'utf-8'
44+
45+
text = "Hello World of Sockets in Python"
46+
data = text.encode('utf-8') # encode string to bytes
47+
s.send(data)
48+
49+
print('[{}] send: {}'.format(now, text))
50+
51+
# --- receive data ---
52+
53+
# if you don't use native characters
54+
# then you can use 'ascii' instead of 'utf-8'
55+
56+
data = s.recv(1024)
57+
text = data.decode('utf-8') # decode bytes to string
58+
59+
print('[{}] recv: {}'.format(now, text))
60+
61+
# --- wait awhile ---
62+
63+
time.sleep(1)
64+
65+
except Exception as e:
66+
print('[DEBUG] exception:', e)
67+
68+
# --- close socket ---
69+
70+
print('[DEBUG] close socket')
71+
72+
s.close()

socket/basic/version-4/server.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# https://docs.python.org/3.5/library/socket.html
5+
#
6+
7+
import socket
8+
import threading
9+
import time
10+
11+
# --- constants ---
12+
13+
HOST = '' # local address IP (not external address IP)
14+
15+
# '0.0.0.0' or '' - conection on all NICs (Network Interface Card),
16+
# '127.0.0.1' or 'localhost' - local conection only (can't connect from remote computer)
17+
# 'local_IP' - connection only on one NIC which has this IP
18+
19+
PORT = 8000 # local port (not external port)
20+
21+
22+
# --- functions ---
23+
24+
def handle_client(conn, addr):
25+
26+
try:
27+
while True:
28+
# --- receive/send data ---
29+
30+
# if client first `send()` and next `recv()`
31+
# then server have to first `recv`() and next `send()`
32+
33+
# if both will `recv()` at the same time then all will hang
34+
# because both will wait for data and nobody will `send()`
35+
36+
# if you don't use native characters
37+
# then you can use 'ascii' instead of 'utf-8'
38+
39+
now = int(time.time())
40+
41+
# receiving
42+
43+
data = conn.recv(1024)
44+
text = data.decode('utf-8') # decode bytes to string
45+
46+
print('[{}][{}] recv: {}'.format(addr, now, text))
47+
48+
# sending
49+
50+
text = 'Thank you [{}]'.format(now)
51+
data = text.encode('utf-8') # encode string to bytes
52+
conn.send(data)
53+
54+
print('[{}]][{}] send: {}'.format(addr, now, text))
55+
56+
except Exception as e:
57+
print('[DEBUG] exception:', e)
58+
59+
# --- create socket ---
60+
61+
print('[DEBUG] create socket')
62+
63+
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
64+
s = socket.socket() # default value is (socket.AF_INET, socket.SOCK_STREAM)
65+
# so you don't have to use it in socket()
66+
67+
# --- options ---
68+
69+
print('[DEBUG] set options')
70+
71+
# solution for "[Error 89] Address already in use". Use before bind()
72+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
73+
74+
# --- assign socket to local IP (local NIC) ---
75+
76+
print('[DEBUG] bind:', (HOST, PORT))
77+
78+
s.bind((HOST, PORT)) # one tuple (HOST, PORT), not two arguments
79+
80+
# --- set size of queue ---
81+
82+
print('[DEBUG] listen')
83+
84+
s.listen(1) # number of clients waiting in queue for "accept".
85+
# If queue is full then client can't connect.
86+
87+
while True:
88+
# --- accept client ---
89+
90+
# accept client and create new socket `conn` (with different port) for this client only
91+
# and server will can use `s` to accept other clients (if you will use threading)
92+
93+
print('[DEBUG] accept ... waiting')
94+
95+
conn, addr = s.accept()
96+
97+
print('[DEBUG] addr:', addr)
98+
99+
# --- run thread ---
100+
101+
t = threading.Thread(target=handle_client, args=(conn, addr))
102+
t.start()
103+
104+
# --- close all sockets ---
105+
106+
# alway first close `conn`, next close `s`
107+
108+
print('[DEBUG] close socket(s)')
109+
110+
conn.close()
111+
s.close()
File renamed without changes.
File renamed without changes.
File renamed without changes.

thread/simple-example/main.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
#!/usr/bin/env python3
22

3-
from threading import Thread
3+
#
4+
# https://docs.python.org/3/library/threading.html
5+
#
6+
7+
import threading
48
import time
59

6-
# -----
10+
# --- classes ---
11+
12+
class ExampleThread(threading.Thread):
13+
14+
def __init__(self, name, counter=10, sleep=0.5):
15+
threading.Thread.__init__(self)
16+
self.name = name
17+
self.counter = counter
18+
self.sleep = sleep
19+
20+
def run(self):
21+
for x in range(self.counter):
22+
print(self.name, x)
23+
time.sleep(self.sleep)
24+
25+
# --- functions ---
726

8-
def my_function(name, counter):
27+
def example_function(name, counter=10, sleep=0.5):
928
for x in range(counter):
1029
print(name, x)
11-
time.sleep(0.5)
30+
time.sleep(sleep)
1231

13-
# -----
1432

15-
class MyThread(Thread):
16-
17-
def run(self):
18-
my_function("Hello", 10)
19-
20-
# -----
33+
# --- example 1 ---
34+
35+
# `args` have to be tuple.
36+
# for one argument you need `args=("function:",)`
37+
38+
t1 = threading.Thread(target=example_function, args=("function:", 15))
39+
t1.start()
40+
41+
# --- example 2 ---
42+
43+
t2 = ExampleThread("class:", 10, 1.0)
44+
t2.start()
2145

22-
thread = MyThread()
23-
thread.start()
46+
# --- example 2 ---
2447

25-
# -----
48+
# start thread after 3 seconds
2649

27-
# args have to be tuple so for one argument `args=(10,)`
50+
t3 = threading.Timer(3, example_function, args=("** timer **:", 2, 3.0))
51+
t3.start()
2852

29-
thread = Thread( target=my_function, args=("World", 10) )
30-
thread.start()
3153

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

zella-graphics/arc-chord-pie/eye-2.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from graphics import *
2+
3+
# --- constants ---
4+
5+
WIDTH = 300
6+
HEIGHT = 300
7+
8+
# --- main ----
9+
10+
win = GraphWin("EXAMPLE", WIDTH, HEIGHT)
11+
12+
# ---
13+
14+
RADIUS = 50
15+
16+
bbox = (0, -RADIUS/2, RADIUS*2, RADIUS*2-RADIUS/2)
17+
win.create_arc(bbox, fill="blue", outline="blue", extent=120, style='chord', start=30+180)
18+
19+
bbox = (0, RADIUS/2, RADIUS*2, RADIUS*2+RADIUS/2)
20+
win.create_arc(bbox, fill="blue", outline="blue", extent=120, style='chord', start=30)
21+
22+
#bbox = (0, -75, RADIUS*2, RADIUS*2-75)
23+
#win.create_arc(bbox, fill="blue", outline="blue", extent=120, style='chord', start=30+180)
24+
25+
#bbox = (0, 75, 300, 300+75)
26+
#win.create_arc(bbox, fill="blue", outline="blue", extent=120, style='chord', start=30)
27+
28+
# ---
29+
30+
#win.create_oval((100, 100, 200, 200), fill="white", outline="white")
31+
#win.create_oval((130, 130, 170, 170), fill="black", outline="black")
32+
33+
# ---
34+
#win.getKey()
35+
win.getMouse()
36+
37+
win.close()

zella-graphics/arc-chord-pie/eye.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from graphics import *
2+
3+
# --- constants ---
4+
5+
WIDTH = 300
6+
HEIGHT = 300
7+
8+
# --- main ----
9+
10+
win = GraphWin("EXAMPLE", WIDTH, HEIGHT)
11+
12+
win.create_arc((0, -75, 300, 300-75), fill="blue", outline="blue", extent=120, style='chord', start=30+180)
13+
win.create_arc((0, 75, 300, 300+75), fill="blue", outline="blue", extent=120, style='chord', start=30)
14+
15+
# ---
16+
17+
RADIUS = 75
18+
19+
bbox = (0, -75, 300, 300-75)
20+
win.create_arc(bbox, fill="blue", outline="blue", extent=120, style='chord', start=30+180)
21+
bbox = (0, 75, 300, 300+75)
22+
win.create_arc(bbox, fill="blue", outline="blue", extent=120, style='chord', start=30)
23+
24+
# ---
25+
26+
win.create_oval((100, 100, 200, 200), fill="white", outline="white")
27+
win.create_oval((130, 130, 170, 170), fill="black", outline="black")
28+
29+
# ---
30+
#win.getKey()
31+
win.getMouse()
32+
33+
win.close()
Loading
4 KB
Loading
4.54 KB
Loading

zella-graphics/arc-chord-pie/main.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from graphics import *
2+
3+
# --- constants ---
4+
5+
WIDTH = 300
6+
HEIGHT = 300
7+
8+
# --- functions ---
9+
10+
def moves():
11+
12+
# move figure 1
13+
s = win.itemcget(fig1, 'start')
14+
win.itemconfig(fig1, start=float(s)+5)
15+
16+
# move figure 2
17+
s = win.itemcget(fig2, 'start')
18+
win.itemconfig(fig2, start=float(s)+5)
19+
20+
# move figure 3
21+
s = win.itemcget(fig3, 'start')
22+
win.itemconfig(fig3, start=float(s)+5)
23+
24+
# run again after 100ms (0.1s)
25+
win.after(100, moves)
26+
27+
# --- main ----
28+
29+
win = GraphWin("Patch", WIDTH, HEIGHT)
30+
31+
bbox = (5, 5, WIDTH-5, HEIGHT-5)
32+
33+
#fig1 = win.create_arc(bbox, fill="red", outline='green', width=3, start=0, extent=90, style='arc')
34+
#fig2 = win.create_arc(bbox, fill="red", outline='green', width=3, start=95, extent=90, style='chord')
35+
#fig3 = win.create_arc(bbox, fill="red", outline='green', width=3, start=190, extent=90, style='pieslice')
36+
37+
win.create_arc((0, -75, 300, 225), fill="blue", outline="blue", extent=120, style='chord', start=30+180)
38+
win.create_arc((0, 75, 300, 375), fill="blue", outline="blue", extent=120, style='chord', start=30)
39+
40+
# run first time
41+
#moves()
42+
43+
#win.getKey()
44+
win.getMouse()
45+
46+
win.close()

0 commit comments

Comments
 (0)