Raspithek
2 years ago
14 changed files with 783 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,15 @@ |
|||
# fib.py |
|||
import sys |
|||
|
|||
# berechnet rekursiv die Fibonaccizahl n. |
|||
def fib(n): |
|||
|
|||
# Rekursionsbedingung |
|||
if n in [0,1]: |
|||
return 1 |
|||
else: |
|||
# Rekursionsaufruf |
|||
return fib(n-1) + fib(n-2) |
|||
|
|||
element = int(sys.argv[1]) |
|||
print(fib(element)) |
@ -0,0 +1,60 @@ |
|||
# encoding: utf-8 |
|||
# fibclient.py |
|||
import socket |
|||
import time |
|||
from network.const import __ADDRESS__ |
|||
|
|||
DISCONNECT_MESSAGE = "!DISCONNECT" |
|||
|
|||
# Verbindung herstellen |
|||
def connect(): |
|||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|||
client.connect(__ADDRESS__) |
|||
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() |
@ -0,0 +1,92 @@ |
|||
#! /usr/bin/python3 |
|||
|
|||
import socket |
|||
import threading |
|||
from network.const import __ADDRESS__ |
|||
|
|||
# Server-Socket aufbauen |
|||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|||
|
|||
#Socket an unsere Server-Adresse binden |
|||
server.bind(__ADDRESS__) |
|||
|
|||
# Set mit den verbundenen Clients |
|||
clients = set() |
|||
|
|||
# Locks für den Zugriff auf das Set |
|||
clients_lock = threading.Lock() |
|||
|
|||
# Unsere Arbeitsmethode: rekursive Fibonacci-Reihe |
|||
def fib(n): |
|||
|
|||
# Rekursionsbedingung |
|||
if n in [0,1]: |
|||
return 1 |
|||
else: |
|||
# Rekursionsaufruf |
|||
return fib(n-1) + fib(n-2) |
|||
|
|||
#bearbeitet eine eingehende Connection annehmen, pro Client ein seperater Thread |
|||
def handle_connection(conn,addr): |
|||
print(f"[NEW CONNECTION] {addr} Connected") |
|||
|
|||
try: |
|||
connected = True |
|||
while connected: |
|||
msg = conn.recv(64).decode('utf-8') |
|||
print(f'[RECEIVED] {msg}') |
|||
if not msg: |
|||
break |
|||
else: |
|||
if msg.startswith('fib:'): |
|||
element = int(msg[4:]) |
|||
fibo = fib(element) |
|||
print(f'[SENDING] fib({element})={fibo}') |
|||
# Ergebnis an Client senden. |
|||
with clients_lock: |
|||
for c in clients: |
|||
c.sendall(f'{fibo}'.encode('utf-8')) |
|||
else: |
|||
pass |
|||
|
|||
if msg == 'quit': |
|||
print('[DISCONNECT]') |
|||
connected = False |
|||
|
|||
# with clients_lock: |
|||
# for c in clients: |
|||
# c.sendall(f"[{addr}] {msg}".encode('utf-8')) |
|||
|
|||
finally: |
|||
# Clientverbindung abbauen und aus Set entfernen. |
|||
with clients_lock: |
|||
clients.remove(conn) |
|||
|
|||
conn.close() |
|||
|
|||
|
|||
# Start des Servers |
|||
def start(): |
|||
print('[SERVER STARTED]!') |
|||
# Horchen nach eingehender Verbindung |
|||
server.listen() |
|||
while True: |
|||
# Neue Verbindung annehmen |
|||
|
|||
conn, addr = server.accept() |
|||
# Neuen Client in Set aufnehmen |
|||
with clients_lock: |
|||
print(f'[ADDING CONNECTION] {conn}') |
|||
clients.add(conn) |
|||
|
|||
#Thread zur Bearbeitung des Requests starten. |
|||
thread = threading.Thread(target=handle_connection, args=(conn, addr)) |
|||
thread.start() |
|||
|
|||
#Hauptprogramm: Serverstart einleiten |
|||
if __name__ == '__main__': |
|||
start() |
|||
|
|||
|
|||
|
|||
|
@ -0,0 +1,11 @@ |
|||
import requests |
|||
|
|||
url = requests.get('https://raspithek.de') |
|||
|
|||
|
|||
data = url.text |
|||
|
|||
|
|||
print(data) |
|||
|
|||
|
@ -0,0 +1,20 @@ |
|||
# encoding: UTF-8 |
|||
import urllib.request |
|||
|
|||
# URL öffnen und Daten anfordern. Ohne spezielle Dateiangabe |
|||
# liefert der Webserver immer die index.html zurück. |
|||
fp = urllib.request.urlopen('https://raspithek.de') |
|||
|
|||
# Daten auslesen |
|||
data = fp.read() |
|||
|
|||
print(type(data)) |
|||
# Die Daten sin vom Datentyp Bytes und müssen daher erst |
|||
# zum UTF-8 String umkodiert werden. |
|||
str = data.decode('utf8') |
|||
|
|||
fp.close() |
|||
|
|||
print(str) |
|||
|
|||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1 @@ |
|||
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,10 @@ |
|||
def receive(conn): |
|||
msg = conn.recv(64).decode('utf-8') |
|||
|
|||
return msg |
|||
|
|||
def send(client, msg): |
|||
message = msg.encode('utf-8') |
|||
client.send(message) |
|||
|
|||
|
@ -0,0 +1,12 @@ |
|||
#network/const.py |
|||
|
|||
import socket |
|||
|
|||
# TCP Port |
|||
__PORT__ = 6554 |
|||
|
|||
#Server-IP |
|||
__SERVER__ = socket.gethostbyname(socket.gethostname()) |
|||
|
|||
# Server-Adresse (IP,Port) |
|||
__ADDRESS__ = (__SERVER__,__PORT__) |
@ -0,0 +1,3 @@ |
|||
|
|||
|
|||
print('fib:58'[4:]) |
@ -0,0 +1,5 @@ |
|||
from network.const import __SERVER__ |
|||
from network.const import __ADDRESS__ |
|||
|
|||
print(f'Server:{__SERVER__}') |
|||
print(f'Address:{__ADDRESS__}') |
Loading…
Reference in new issue