Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python servers send specific info from post requests

I create a simple server in replit.com and I’m able get people post information from client but not able to send specific data to client from my server. I also have a domain name, and this only works for IPV6.
website code(https://replit.com/@alexhernandez11/server)

server

import socket
import urllib.request

external_ip = urllib.request.urlopen('https://ident.me').read().decode('utf8')
print("v6 ip:",external_ip)

#socket AF_INET = v4
#socket AF_INET6 = v6
sock = socket.socket(socket.AF_INET6 , socket.SOCK_STREAM)
#s.bind(('',55555))
sock.listen(100)

while True:
    clients = []
    while True:

        clientsocket , address = sock.accept()
      
        clients.append(clientsocket)
        if len(clients) == 2:
            print('got 2 clients, sending details to each')
            break
        data = clientsocket.recv(1024)
        print("data :",data.decode())
        clientsocket.send(bytes(str("hello my dudes"),"utf-8"))
      
  
    c1 = clients.pop()
    c1_addr_port= c1
    c2 = clients.pop()
    c1_addr_port = c2


    c1.send(bytes(str("hello my dudes"),"utf-8"))
    c2.send(bytes(str("hello my dudes"),"utf-8"))

client

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import requests 
url = 'https://server.alexhernandez11.repl.co'
#x = requests.get(url)

my_text = 'hello sending info121'
info = my_text.encode('utf-8')

x = requests.post(url, data = info)

print(x.content)
#r = requests.post('https://server.alexhernandez11.repl.co / post', data ="testin ")

>Solution :

Well, you’re running a TCP server and sending HTTP requests to it, so they except you to return proper HTTP response, not just some bytes.

This is what server looks like on my PC (obviously, with IPv4 and dirty code):

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 55555))
sock.listen(100)

RESPONSE = """HTTP/1.1 200 OK\r\n\
Date: Mon, 27 Jul 2009 12:28:53 GMT\r\n\
Server: Apache/2.2.14 (Win32)\r\n\
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\r\n\
Content-Length: {length}\r\n\
Content-Type: text/html\r\n\
Connection: Closed\r\n\r\n\
{body}
"""

while True:
    clientsocket, address = sock.accept()
    data = clientsocket.recv(1024)
    print("data :", data.decode())

    text = "hello dude"
    content_length = len(text.encode("utf-8"))
    response = RESPONSE.format(length=content_length, body=text)
    clientsocket.send(response.encode("utf-8"))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading