User Tools

Site Tools


rx3:index

Réseau L2 Info : Socket en Python 3

Client DayTime en UDP

#!/usr/bin/python3
import sys
import socket
 
HOST = 'time-c.nist.gov'
PORT = 13                  # daytime  
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(b'', (HOST,PORT))
d = s.recvfrom(1024)
reply = d[0]
addr = d[1]
print 'Server reply : ' + reply
s.close()
print ('Received', data)

Client HTTP/GET (TCP)

#!/usr/bin/python3
import sys
import socket
 
HOST = 'www.labri.fr'     
PORT = 80                 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'GET /\r\n\r\n')
data = s.recv(1024)
s.close()
print ('Received', data)

Serveur Echo UDP

Voici un echo server en version UDP…

#!/usr/bin/python3
import socket
import sys
 
HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 7777               # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)    # ipv4 only
# s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) # ipv4/ipv6
s.bind((HOST, PORT))
 
while True:
  reply, addr = s.recvfrom(1500)
  print (reply)  
  s.sendto(reply, addr)

On utilise netstat pour vérifier que son serveur écoute :

netstat -ulpn
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  udp        0      0 0.0.0.0:7777            0.0.0.0:*                           3314/python3        

Voici un client netcat ipv4/udp:

nc -4 -u localhost 7777
 coucou
 coucou

Serveur TCP

Voici un echo server en version TCP…

#!/usr/bin/python3
import socket
 
HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 7777               # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(0)
while True:
    sclient, addr = s.accept()
    print('Connected by', addr)
    while True:
        data = sclient.recv(1500)
        if data == b'' or data == b'\n' : break
        print(data)
        sclient.sendall(data)
    print('Disconnected by', addr)        
    sclient.close()

Serveur Echo TCP (version select)

La version select permet de gérer de multiples clients simultanément.

#!/usr/bin/python3
import socket
import select
 
HOST = ''
PORT = 7777
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen()
l = []
l.append(s)
m = {}
 
while True:
    l2, _, _ = select.select(l, [], [])
    for s2 in l2:
        # socket server (new client connection)
        if s2 == s:  
            sclient, addr = s.accept()
            l.append(sclient)
            m[sclient] = addr
            print('Connected by', addr)
        # socket client (new client message)
        else:        
            while True:
                data = s2.recv(1500)
                print(data)
                if data == b'' or data == b'\n' :
                    print('Disconnected by', m[s2])
                    l.remove(s2)
                    s2.close()
                    break
                s2.sendall(data)                  
s.close()

Documentation

rx3/index.txt · Last modified: 2017/03/24 14:29 by orel