M
M
Maratk1n2017-08-09 14:42:10
Python
Maratk1n, 2017-08-09 14:42:10

How to make friends Python and Callback API?

I can't figure out what I'm doing wrong.
I connected my vps to VK, where jsons are now constantly coming, because I can't answer correctly.
Here is what is written in the documentation :

Обратите внимание: после получения уведомления Ваш сервер должен возвращать строку "ok" 
и статус HTTP 200. Если сервер несколько раз подряд вернет ошибку, Callback API временно 
перестанет отправлять на него уведомления.

Accordingly, the server code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import logging
from time import sleep

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
        sock.bind(('0.0.0.0', 80))
except Exception:
        logging.exception('processing request')
sock.listen(1)

#conn, addr = sock.accept()
#print('connected:'+str(addr))
ans = b"2fc3154"
status = b"HTTP/1.1 200 ok"
while True:
        conn, addr = sock.accept()
        print('connected:'+str(addr))
        print(conn)
        #conn.settimeout(5)
        data = conn.recv(512)
        if not data:
                break
        print(data)
        #conn.send(b'ok\n')
        conn.send(bytes('HTTP/1.1 200 OK\nConnection: close\n\n', 'UTF-8'))
        conn.send(b'ok')
        #break
conn.close()
sock.close()

The browser displays only "ok" without headers. VK sees headers (tried to send HTTP 404).
The problem is that VK does not accept server responses and throws out timeout errors mixed with an incorrect server response error (Incorrect response text).
What can I do wrong..?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question