> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# So änderst du PTB von httpx zu aiohttp

> Dieses Tutorial zeigt dir, wie du httpx in python-telegram-bot zu aiohttp änderst.

## Einführung

* Dieser Artikel führt dich durch die Umstellung von `httpx` auf `aiohttp` in `python-telegram-bot`. `httpx` und `aiohttp` sind Bibliotheken, die für HTTP-Anfragen verwendet werden.
* Bevor wir beginnen, stelle sicher, dass Python und die Bibliothek python-telegram-bot in deiner Umgebung installiert sind. Sieh dir den Installationsbefehl für python-telegram-bot unten an.

```bash theme={null}
pip install python-telegram-bot
```

## Bibliotheken ändern

### Installation

* Zunächst musst du eine Bibliothek installieren, die eine Klasse zur Verarbeitung der an Telegram gesendeten Anfragen bereitstellt. Lass uns `ptbcontrib` installieren, verwende den folgenden Befehl:

```bash theme={null}
pip install git+https://github.com/python-telegram-bot/ptbcontrib.git@main
```

### httpx zu aiohttp ändern

* Als Nächstes müssen wir `AiohttpRequest` in der Datei importieren, in der wir unseren Bot-Client instanziieren.

```python theme={null}
from ptbcontrib.aiohttp_request import AiohttpRequest
```

* Diese Klasse verarbeitet alle Anfragen an Telegram anstelle des standardmäßigen `httpx`.
* `AiohttpRequest` wird in der PTB-Client-Instanz wie im folgenden Beispiel verwendet:

<CodeGroup>
  ```python Bot theme={null}
  import asyncio
  import telegram
  from ptbcontrib.aiohttp_request import AiohttpRequest


  async def main():
      bot = telegram.Bot("TOKEN", request=AiohttpRequest(), get_updates_request=AiohttpRequest())
      async with bot:
          print(await bot.get_me())


  if __name__ == '__main__':
      asyncio.run(main())
  ```

  ```python ApplicationBuilder theme={null}
  import logging
  from telegram import Update
  from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
  from ptbcontrib.aiohttp_request import AiohttpRequest

  logging.basicConfig(
      format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
      level=logging.INFO
  )

  async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
      await context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")

  if __name__ == '__main__':
      application = ApplicationBuilder().request(AiohttpRequest(connection_pool_size=256)).get_updates_request(AiohttpRequest()).token('TOKEN').build()
      
      start_handler = CommandHandler('start', start)
      application.add_handler(start_handler)
      
      application.run_polling()
  ```
</CodeGroup>

## Warum diese Änderung vornehmen?

* Die Umstellung der Bibliothek httpx auf aiohttp bringt einige Vorteile mit sich.

1. **Leistung**: aiohttp ist schneller als httpx.
2. **Fehlerminderung**: httpx löst aufgrund seiner Konfigurationen und Leistung zu viele Netzwerkfehler wie ReadError und andere NetworkErrors aus.

## Hat Ihnen dieser Artikel gefallen?

* Wir haben diesen Inhalt mit großer Sorgfalt erstellt, um Ihnen die bestmögliche Hilfe zu bieten.
  Wenn Ihnen dieser Artikel in irgendeiner Weise geholfen hat, unterstützen Sie unsere Arbeit! Hinterlassen Sie Ihre Bewertung! Sie hilft uns zu verstehen, was Ihnen am wichtigsten ist.

<CardGroup cols={2}>
  <Card title="Google Reviews" icon="google" href="https://g.page/r/CYZenpQcDgTzEAI/review">
    Hinterlassen Sie Ihre Bewertung bei Google Reviews.
  </Card>

  <Card title="Trustpilot" icon="star" href="https://www.trustpilot.com/review/squarecloud.app">
    Hinterlassen Sie Ihre Bewertung bei Trustpilot.
  </Card>
</CardGroup>
