|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {
- "pycharm": {
- "name": "#%% md\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "# Software Entwicklung \n",
- "\n",
- "## Kapitel 9: IO\n",
- "\n",
- "### 9.5 Sockets"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "pycharm": {
- "name": "#%% md\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "#### 9.5.3 Beispiel für einen Server"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "pycharm": {
- "name": "#%%\n"
- }
- },
- "outputs": [],
- "source": [
- "import socket\n",
- "\n",
- "with socket.socket() as server: # Generieren eines Sockets\n",
- " host = socket.gethostname() # Die eigene IP-Nummer\n",
- " port = 12345 # Festlegen des Ports\n",
- " server.bind((host, port)) # Bind\n",
- " server.listen() # Listen (blockiert nicht)\n",
- " print(\"Ich lausche...\")\n",
- "\n",
- " for contact in range(0, 10):\n",
- " connection, addr = server.accept() # blockiert\n",
- " with connection:\n",
- " print('Verbindung von ', addr)\n",
- " msg = f'Danke für das Vorbeischauen, #{contact+1}!\\n'\n",
- " connection.send(msg.encode('utf-8')) # Senden \n",
- "\n",
- " print(\"Das wars!\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "pycharm": {
- "name": "#%% md\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "Textnachrichten, die über ein Socket versendet werden sollen, müssen in Bytes\n",
- "umgewandelt werden.\n"
- ]
- }
- ],
- "metadata": {
- "celltoolbar": "Slideshow",
- "kernelspec": {
- "display_name": "Python 3 (ipykernel)",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.9.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|