You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
65 lines
1.5 KiB
# encoding: utf-8
|
|
# fibclient.py
|
|
import socket
|
|
import time
|
|
#from network.const import __ADDRESSREMOTE__
|
|
#from network.const import __ADDRESSLOCAL__
|
|
from network import __ADDRESSLOCAL__
|
|
|
|
DISCONNECT_MESSAGE = "!DISCONNECT"
|
|
|
|
# Verbindung herstellen
|
|
def connect():
|
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
# für fibserver auf einem Remote-Pi
|
|
# client.connect(__ADDRESSREMOTE__)
|
|
# für fibserver auf lokalem Pi (import oben beachten!)
|
|
client.connect(__ADDRESSLOCAL__)
|
|
return client
|
|
|
|
# msg senden
|
|
def send(client, msg):
|
|
message = msg.encode('utf-8')
|
|
client.send(message)
|
|
|
|
# Daten empfangen
|
|
def receive(conn):
|
|
msg = conn.recv(64).decode('utf-8')
|
|
|
|
return msg
|
|
|
|
# Hauptmethode
|
|
def start():
|
|
# String von Console lesen
|
|
answer = input('Verbindung aufbauen? (ja/nein)? ')
|
|
if answer.lower() != 'ja':
|
|
return
|
|
|
|
# Verbindung aufbauen
|
|
connection = connect()
|
|
while True:
|
|
msg = input("Nachricht (q für quit): ")
|
|
|
|
# Bei Eingabe == 'q' Schleife beenden.
|
|
if msg == 'q':
|
|
break
|
|
# Sonst eingegeben String an Server senden.
|
|
send(connection, msg)
|
|
# und auf Ergebnis warten.
|
|
result = receive(connection)
|
|
print(f'result={result}')
|
|
|
|
# Fibonaccizahl isolieren.
|
|
fib = int(result.split('[')[0])
|
|
print(f'Fibonnaci-Zahl ist {fib}')
|
|
print(f'[RESULT] {result}')
|
|
|
|
|
|
# zum Beenden die Nachricht 'quit an den Server senden
|
|
send(connection, 'quit')
|
|
time.sleep(1)
|
|
print('Disconnected')
|
|
|
|
# Hauptprogramm: Client starten
|
|
if __name__ == '__main__':
|
|
start()
|
|
|