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 [26.09.2019 19:50] 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 122: Zeile 158:
         del message_queues[s]         del message_queues[s]
 </file> </file>
 +
 +===== 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>
 +#!/usr/bin/env python3
 +
 +import sys, termios, tty, os, time
 + 
 +def getch():
 +    fd = sys.stdin.fileno()
 +    old_settings = termios.tcgetattr(fd)
 +    try:
 +        tty.setraw(sys.stdin.fileno())
 +        ch = sys.stdin.read(1)
 + 
 +    finally:
 +        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
 +    return ch
 +
 +while True:
 +    char = getch()
 + 
 +    if (char == "p"):
 +        print("Stop!")
 +        exit(0)
 + 
 +    if (char == "a"):
 +        print("Left pressed")
 +        time.sleep(1)
 +</file>
 +
 +
 +
 +
 +
  
lager/lok_netze/tcp_python.1569520222.txt.gz · Zuletzt geändert: 26.09.2019 19:50 von richard

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki