Skip to content

Commit 9526c9d

Browse files
committed
other
1 parent 54d10ee commit 9526c9d

18 files changed

Lines changed: 339 additions & 17 deletions

File tree

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.

0 commit comments

Comments
 (0)