Benutzer-Werkzeuge

Webseiten-Werkzeuge


lager:lok_netze:tcp_python

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
lager:lok_netze:tcp_python [01.11.2019 09:18] richardlager:lok_netze:tcp_python [30.04.2023 15:46] (aktuell) – Status der Diskussion geändert richard
Zeile 1: Zeile 1:
-~~DISCUSSION|Ergänzungen~~+~~DISCUSSION:closed|Ergänzungen~~
 ====== Programmierung Übung zu TCP mit python ====== ====== Programmierung Übung zu TCP mit python ======
  
Zeile 63: Zeile 63:
 print('Received', repr(data)) print('Received', repr(data))
 </file> </file>
 +
 +==== Interaktiver Echo-Client ====
 +
 +<file python echo_client_interactive.py>
 +#!/usr/bin/env python3
 +
 +import socket
 +
 +HOST = '127.0.0.1'  # Hostname oder IP des Echo-Servers eintragen
 +# PORT = 65432        # Port des Servers eintragen
 +PORT = 50000
 +
 +host = input('Server-IP [' + HOST + ']: ')
 +port = input('Server-Port [' + str(PORT) + ']: ')
 +
 +if host != '':
 +    HOST = host
 +if port != '':
 +    PORT = int(port, 10)
 +
 +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
 +    s.connect((HOST, PORT))
 +    written = ''
 +    while True:
 +        written = input('Your message (type "exit" to exit): ')
 +        if written == 'exit':
 +            break;
 +        s.sendall(bytearray(written, 'UTF-8'))
 +        data = s.recv(1024)
 +        print('Received ', repr(data))
 + 
 +print('Client closed')
 +</file>
 +
  
  
Zeile 76: Zeile 110:
  
 <file python tcp_multi_server.py> <file python tcp_multi_server.py>
 +#!/usr/bin/env python3
 +
 import select, socket, sys, queue import select, socket, sys, queue
 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Zeile 125: Zeile 161:
 ===== Keystroke als Command Line Handler ===== ===== Keystroke als Command Line Handler =====
  
 +ACHTUNG: Das folgende Beispiel funktioniert nur, wenn das Script direkt in der Shell ausgeführt wird. Hierzu muss zunächst die Datei ausführbar gemacht werden. Mit folgendem Befehl kann dies getan werden:
 +
 +<code>chmod +x keystroke_sample.py</code>
 +
 +Das folgende Bespiel wartet dauerhaft auf eine Eingabe und führt je nach Tastedruck Programmcode aus.
 <file python keystroke_sample.py> <file python keystroke_sample.py>
 +#!/usr/bin/env python3
 +
 import sys, termios, tty, os, time import sys, termios, tty, os, time
    
Zeile 150: Zeile 193:
         time.sleep(1)         time.sleep(1)
 </file> </file>
 +
 +
 +
 +
  
  
lager/lok_netze/tcp_python.1572596336.txt.gz · Zuletzt geändert: 01.11.2019 09:18 von richard

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki