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.

096 Server.ipynb 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {
  6. "pycharm": {
  7. "name": "#%% md\n"
  8. },
  9. "slideshow": {
  10. "slide_type": "slide"
  11. }
  12. },
  13. "source": [
  14. "# Software Entwicklung \n",
  15. "\n",
  16. "## Kapitel 9: IO\n",
  17. "\n",
  18. "### 9.5 Sockets"
  19. ]
  20. },
  21. {
  22. "cell_type": "markdown",
  23. "metadata": {
  24. "pycharm": {
  25. "name": "#%% md\n"
  26. },
  27. "slideshow": {
  28. "slide_type": "slide"
  29. }
  30. },
  31. "source": [
  32. "#### 9.5.3 Beispiel für einen Server"
  33. ]
  34. },
  35. {
  36. "cell_type": "code",
  37. "execution_count": null,
  38. "metadata": {
  39. "pycharm": {
  40. "name": "#%%\n"
  41. }
  42. },
  43. "outputs": [],
  44. "source": [
  45. "import socket\n",
  46. "\n",
  47. "with socket.socket() as server: # Generieren eines Sockets\n",
  48. " host = socket.gethostname() # Die eigene IP-Nummer\n",
  49. " port = 12345 # Festlegen des Ports\n",
  50. " server.bind((host, port)) # Bind\n",
  51. " server.listen() # Listen (blockiert nicht)\n",
  52. " print(\"Ich lausche...\")\n",
  53. "\n",
  54. " for contact in range(0, 10):\n",
  55. " connection, addr = server.accept() # blockiert\n",
  56. " with connection:\n",
  57. " print('Verbindung von ', addr)\n",
  58. " msg = f'Danke für das Vorbeischauen, #{contact+1}!\\n'\n",
  59. " connection.send(msg.encode('utf-8')) # Senden \n",
  60. "\n",
  61. " print(\"Das wars!\")"
  62. ]
  63. },
  64. {
  65. "cell_type": "markdown",
  66. "metadata": {
  67. "pycharm": {
  68. "name": "#%% md\n"
  69. },
  70. "slideshow": {
  71. "slide_type": "slide"
  72. }
  73. },
  74. "source": [
  75. "Textnachrichten, die über ein Socket versendet werden sollen, müssen in Bytes\n",
  76. "umgewandelt werden.\n"
  77. ]
  78. }
  79. ],
  80. "metadata": {
  81. "celltoolbar": "Slideshow",
  82. "kernelspec": {
  83. "display_name": "Python 3 (ipykernel)",
  84. "language": "python",
  85. "name": "python3"
  86. },
  87. "language_info": {
  88. "codemirror_mode": {
  89. "name": "ipython",
  90. "version": 3
  91. },
  92. "file_extension": ".py",
  93. "mimetype": "text/x-python",
  94. "name": "python",
  95. "nbconvert_exporter": "python",
  96. "pygments_lexer": "ipython3",
  97. "version": "3.9.9"
  98. }
  99. },
  100. "nbformat": 4,
  101. "nbformat_minor": 1
  102. }