0

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

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 ")

1 Answers1

1

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"))

Daniel
  • 202
  • 1
  • 3
  • nice it works one more question how I properly read the post request sometimes I receive it sometimes I don't ("Content-Length: 21") – alex hernandez Aug 13 '22 at 01:57
  • 1
    If your server just cannot recieve a request, then I can't tell what the reason is specifically, maybe because of replit itself, maybe your server is busy working with another client. If you want to parse an HTTP request, you might wanna use some third-party library for that or see how it is done by hand here: https://stackoverflow.com/questions/39090366/how-to-parse-raw-http-request-in-python-3 And `Content-Length` header contains the number of bytes of data passed in the body (i.e. below the headers and a blank line): https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length – Daniel Aug 13 '22 at 02:05