ErmöglichenPython

Python-Programme
Anonymous
 Ermöglichen

Post by Anonymous »

Ich hatte ein Projekt mit Python und einer Postgres -Datenbank unter Windows installiert. Jetzt muss ich dies auf Ubuntu übertragen und die Python -Datei, die ich zuvor verwendet habe, in einen Docker -Container verwandeln. Ich habe versucht, dies zu tun, aber für mich funktioniert etwas nicht. Zuerst werde ich den Inhalt meines Projekts beschreiben.

Code: Select all

Dockerfile

[*]

Code: Select all

webhook_receiver.py

Code: Select all

webhook_receiver.py
:

Code: Select all

from flask import Flask,reuqest,jsonify
import psycopg2
import select
import json
import psycopg2.extensions
import requests
import pycopg2.extras as extras
app=Flask(__name__)
@app.route('/webhook',methods=['POST'])
def webhook():
data=request.json
print(f"received webhook data:{data}")
return jsonify({"status":"success"}),200
def webhook_receiver():
conn=psycopg2.connect("host=127.0.0.1 dbname=webhook_db user=postgres password=***** port=5432")
conn.setisolation_level(psycopg2.exrensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()
cur.execute("LISTEN my_channel")
print("Waiting")
while True:
if select.select([conn],[],[],5)==([],[],[]):
continue
conn.poll()
while conn.notifies:
notify=conn.notifies.pop(0)
print(f"Received notification {notify.payload} from {notify.channel}")
response = requests.post('http://localhost:5000/webhook',json=notify.payload)
print(f"API response: {response.status_code} - {response.text}")
if __name__="__main__":
webhook_receiver()
from threading import Thread
Thread(target=webhook_receiver).start()
app.run(host='0.0.0.0', port=5000)
< /code>
Dockerfile
:

Code: Select all

#Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install psycopg2-binary requests flask
EXPOSE 5000
CMD ["python","webhook_receiver.py"]
< /code>
When I run the command docker build -t webhook_receiver .
Alles ist in Ordnung, aber wenn ich den Befehl Docker ausführe -rm -p 5000: 5000 Webhook_Receiver & Der folgende Fehler wird angezeigt:

Code: Select all

psycopg2.OperationalError:connection to server at "127.0.0.1", port 5432 failed:Connection refused. Is the server running on that host and accepting TCP/IP connections?
< /code>
In the postgresql.conf
Datei, gibt es in der Zeile hören. Wie behebe ich diesen Fehler?

Code: Select all

conn=psycopg2.connect("host=172.0.0.1 ....... port=5432")
< /code>
to this:
conn=pyscopg2.connect("host=host.docker.internal ...... port=5432")
< /code>
sometimes after the internal
Ich habe: 5432 , aber der Fehler ändert P>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post