teil13
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								teil13/__pycache__/network.cpython-39.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								teil13/__pycache__/network.cpython-39.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										15
									
								
								teil13/fib.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								teil13/fib.py
									
									
									
									
									
										Normal file
									
								
							@@ -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))
 | 
			
		||||
							
								
								
									
										60
									
								
								teil13/fibclient.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								teil13/fibclient.py
									
									
									
									
									
										Normal file
									
								
							@@ -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()
 | 
			
		||||
							
								
								
									
										92
									
								
								teil13/fibserver.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										92
									
								
								teil13/fibserver.py
									
									
									
									
									
										Executable file
									
								
							@@ -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()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								teil13/get_requests.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								teil13/get_requests.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
import requests
 | 
			
		||||
 | 
			
		||||
url = requests.get('https://raspithek.de')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
data = url.text
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
print(data)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								teil13/get_urllib.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								teil13/get_urllib.py
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										554
									
								
								teil13/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										554
									
								
								teil13/index.html
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										1
									
								
								teil13/network/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								teil13/network/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								teil13/network/__pycache__/__init__.cpython-39.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								teil13/network/__pycache__/__init__.cpython-39.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								teil13/network/__pycache__/const.cpython-39.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								teil13/network/__pycache__/const.cpython-39.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										10
									
								
								teil13/network/code.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								teil13/network/code.py
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								teil13/network/const.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								teil13/network/const.py
									
									
									
									
									
										Normal file
									
								
							@@ -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__)
 | 
			
		||||
							
								
								
									
										3
									
								
								teil13/substring.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								teil13/substring.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
print('fib:58'[4:])
 | 
			
		||||
							
								
								
									
										5
									
								
								teil13/test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								teil13/test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
from network.const import __SERVER__
 | 
			
		||||
from network.const import __ADDRESS__
 | 
			
		||||
 | 
			
		||||
print(f'Server:{__SERVER__}')
 | 
			
		||||
print(f'Address:{__ADDRESS__}')
 | 
			
		||||
		Reference in New Issue
	
	Block a user