rx3:index
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rx3:index [2018/04/06 07:59] – orel | rx3:index [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Réseau L2 Info : Socket en Python 3 ====== | ====== Réseau L2 Info : Socket en Python 3 ====== | ||
+ | |||
+ | |||
+ | Un peu de documentation : | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
==== Tips en Python ==== | ==== Tips en Python ==== | ||
Line 64: | Line 74: | ||
</ | </ | ||
- | Pour aller plus loin : https:// | + | Pour aller plus loin : https:// |
==Un mot sur le flux TCP !== | ==Un mot sur le flux TCP !== | ||
Line 156: | Line 166: | ||
<code python> | <code python> | ||
# | # | ||
- | import sys | ||
import socket | import socket | ||
- | |||
- | HOST = ' | ||
- | PORT = 80 | ||
s = socket.socket(socket.AF_INET, | s = socket.socket(socket.AF_INET, | ||
- | s.connect((HOST, PORT)) | + | s.connect((" |
- | s.sendall(b' | + | s.sendall(b' |
data = s.recv(1024) | data = s.recv(1024) | ||
s.close() | s.close() | ||
Line 229: | Line 235: | ||
sclient.close() | sclient.close() | ||
</ | </ | ||
+ | |||
+ | ==== Serveur TCP (version multithread) ==== | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | import socket | ||
+ | import threading | ||
+ | |||
+ | |||
+ | def handle(addr, | ||
+ | print(' | ||
+ | while True: | ||
+ | data = sclient.recv(1500) | ||
+ | if data == b'' | ||
+ | break | ||
+ | print(data) | ||
+ | sclient.sendall(data) | ||
+ | print(' | ||
+ | sclient.close() | ||
+ | |||
+ | |||
+ | HOST = '' | ||
+ | PORT = 7777 | ||
+ | s = socket.socket(socket.AF_INET, | ||
+ | s.setsockopt(socket.SOL_SOCKET, | ||
+ | s.bind((HOST, | ||
+ | s.listen(0) | ||
+ | while True: | ||
+ | sclient, addr = s.accept() | ||
+ | t = threading.Thread(None, | ||
+ | t.start() | ||
+ | </ | ||
==== Serveur Echo TCP (version select) ==== | ==== Serveur Echo TCP (version select) ==== | ||
Line 271: | Line 309: | ||
s.close() | s.close() | ||
</ | </ | ||
- | ==== Documentation ==== | ||
- | |||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | |||
- | |||
rx3/index.1523001584.txt.gz · Last modified: 2024/03/18 15:05 (external edit)