A
A
A2ROKirill2019-07-23 09:14:07
Python
A2ROKirill, 2019-07-23 09:14:07

How to send http code status in response to a post?

Good afternoon! I am writing an http server to receive notifications from yandex kassa. There is a basic code for creating a simple http server. According to the yandex documentation, in response, I should send an OK 200 response. Please tell me how can I do this
import http.server
import socketserver
from http import HTTPStatus
port = 8080
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", port), Handler) as httpd:
print("serving at port" , port)
httpd.serve_forever()
This is what I now get in response. There is some kind of incomprehensible encoding that I can not decode.
77.75.154.203 - - [23/Jul/2019 08:54:18] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x03\x00ç\x01\x00\x00ã\x03\x03]6¡ ')
77.75.154.203 - - [23/Jul/2019 08:54:18] " ç ã]6¡ Bhp§ÜæÉÎ$Èn¦ïþ¾¢ËÏ>ø dÀ$À( =À&À* k jÀ" 400 -
77.75. 154.203 - - [23/Jul/2019 08:54:28] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x03\x00ç\x01\x00\x00ã\x03\x03]6¡\x13è ]u\x11AÞíسbÀSÉÕ')
77.75.154.203 - - [23/Jul/2019 08:54:28] " ç ã]6¡è]uAÞíسbÀSÉÕ ×¯Cý[wº÷ñ" 400 -
77.75.153.77 - - [23/ Jul/2019 08:55:10] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x03\x00ç\x01\x00\x00ã\x03\x03]6¡=òâa\x9cv\x04h)/ \x8eý2\x02\x04¸-\x8a\x92\x8dØÊì')
77.75.153.77 - - [23/Jul/2019 08:55:10] " ç ã]6¡=òâavh)/ý2¸-ØÊì þ8è) dÀ $À( =À&À* k jÀ"400-
77.75.154.204 - - [23/Jul/2019 08:56:34] code 400, message Bad request version ('v\x00\x00dÀ$À(\x00=À&À*\x00k\x00jÀ')
77.75.154.204 - - [23/Jul/2019 08:56:34] " ç ã]6¡Û¦yúFKÿ|xOU>'oÍMñv dÀ$À( =À&À* k jÀ" 400 -
77.75.153.77 - - [23/Jul/2019 08 :59:23] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x03\x00ç\x01\x00\x00ã\x03\x03]6¢:Áº\x87\x01ÉA-ê\x8a¢øôA ¡çý\xad\xadÅÿ\x80O\x90\x8d"')
77.75.153.77 - - [23/Jul/2019 08:59:23] " ç ã]6¢:ÁºÉA-ê¢øôA¡çýÅÿO"¥ dÀ$ À( =À&À* k jÀ" 400 -
187.74.85.85 - - [23/Jul/2019 09:06:39] "GET / HTTP/1.1" 200 -
77.75.154.204 - - [23/Jul/2019 09:10 :36] code 400, message Bad request syntax ("\x16\x03\x03\x00ç\x01\x00\x00ã\x03\x03]6¤ÚÔª\x91{VËv\x93\x10°w\x9eX\x06H|ÁÀ8 ~À\x02¦'\x81àhê\x00\x00dÀ$À(\x00=À&À*\x00k\x00jÀ")
77.75.154.204 - - [23/Jul/2019 09:10:36] " ç ã]6¤ÚÔª{VËv°wXH|ÁÀ8~À¦'àhê dÀ$À( =À&À* k jÀ" 400 -

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Taus, 2019-07-23
@Taus

The yandex servers try to establish a secure TLS v1.2 connection over https. This is explicitly stated in the documentation . Either you need to set up SSL connections for http.server with a self-signed certificate, or use ready-made solutions.

A
alternativshik, 2019-07-23
@alternativshik

Why this hemorrhoid? Why not take a ready-made solution - the same flask or django?

K
Kapustlo, 2019-07-25
@Kapustlo

Taus has already mentioned one feature of the Yandex API, I believe that he said everything correctly, but I will give an answer to your specific request. You need to create your own request handler class that will inherit from "SimpleHTTPRequestHandler", in which you will override the "do_GET" method (or whatever you need, depending on the request type) and return the required data.

import http.server
import socketserver
from http import HTTPStatus

class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        encoding = "utf-8"
        self.send_response(200)
        self.end_headers()
        self.wfile.write("Your response".encode(encoding))

port = 8080 

Hander = HTTPRequestHandler()

with socketserver.TCPServer(("", port), Handler) as httpd:
print("serving at port", port)
httpd.serve_forever()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question