M
M
Maratk1n2017-08-07 23:30:45
linux
Maratk1n, 2017-08-07 23:30:45

Receiving data on python server (socket)?

I'm trying to connect the Callback API to my server, but I can't process a post request with a python script.
As you can see, requests to the server are coming.

[email protected]:~/vkserver# tcpdump -s 0 -A port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
18:59:06.714024 IP srv203-173-240-87.vk.com.22149 > voland.http: Flags [S], seq 3222190594, win 14600, options [mss 1460,sackOK,TS val 16552907 ecr 0,nop,wscale 9], length 0
E..<)[email protected]
...........
18:59:06.714079 IP voland.http > srv203-173-240-87.vk.com.22149: Flags [S.], seq 661474302, ack 3222190595, win 14480, options [mss 1460,sackOK,TS val 3664387 ecr 16552907,nop,wscale 4], length 0
E..<[email protected]@.Xc....W....PV.'mK.......8............
.7..........
18:59:06.766049 IP srv203-173-240-87.vk.com.22149 > voland.http: Flags [.], ack 1, win 29, options [nop,nop,TS val 16552920 ecr 3664387], length 0
E..4)[email protected]'mK............
.....7..
18:59:06.766125 IP srv203-173-240-87.vk.com.22149 > voland.http: Flags [P.], seq 1:174, ack 1, win 29, options [nop,nop,TS val 16552920 ecr 3664387], length 173
E...)[email protected]'mK......?.....
.....7..POST / HTTP/1.1
User-Agent: curl/7.26.0
Host: 212.237.7.176
Accept: */*
Content-Type: application/json
Content-Length: 43

{"type":"confirmation","group_id":643042}
18:59:07.015510 IP srv203-173-240-87.vk.com.22149 > voland.http: Flags [P.], seq 1:174, ack 1, win 29, options [nop,nop,TS val 16552983 ecr 3664387], length 173
E...)[email protected]'mK............
.....7..POST / HTTP/1.1
User-Agent: curl/7.26.0
Host: 212.237.7.176
Accept: */*
Content-Type: application/json
Content-Length: 43

Python script:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import logging

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)

while True:
        conn, addr = sock.accept()
        print('connected:'+str(addr))
        #conn.settimeout(10)
        data = conn.recv(1024)
        if not data:
                break
        conn.send('2fc3d')
conn.close()

Wrapped bind in try, because the error [ERROR] Address already in use constantly gets out (socket.SO_REUSEADDR does not help). In short, when the client connects to the server, I should return the string "2fc3d", but I hang on the line
conn, addr = sock.accept()
i.e. constantly waiting for a connection. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2017-08-08
@leahch

You are binding on port 80, on which you already have a web server running. Therefore, your bind crashes with an error. Either change the port number in your script, or stop the web server.
And what does "callback to my server" mean?!
If you are trying to call a Python script from an existing web server, then you don’t need a bind at all, but you need something like cgi / wsgi or proxy ... But to be more precise, you need to know what your web server is , and what is its callback api.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question