python not always receiving post requests recv(1024)

284 Views Asked by At

To connect to the server I use my link(https://server.alexhernandez11.repl.co) my code is linked here(https://replit.com/@alexhernandez11/server) my server sends information all the time but, sometimes the client sends information to the server and I get the headers 100 % but the post message does not go through sometimes.

Server

#tcp server
import socket
import urllib.request

# issues with rev()
#https://stackoverflow.com/questions/7174927/when-does-socket-recvrecv-size-return

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)
#sock.bind(('',6920))
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}
"""
total_clients = 0
while True:
    clients = []
 
    while True:

        clientsocket , address = sock.accept()
      
        clients.append(clientsocket)
        #total_clients += total_clients +1
        if len(clients) == 1:
            #print('got 2 clients, sending details to each')
            #total_clients += total_clients +1
            print("client revieve " , )

            data = clientsocket.recv(1024)
            fields = data.decode().split("\r\n")
            fields = fields[1:]
            output = {}
            for field in fields:
              #print("filed", field)
              if ":" in field:
                key, value = field.split(":" , 1)
                output[key] = value
                pass
                
            #print("data json :", output)

            if "client_info" in output:
              print(output["client_info"])
           
            break
       
        #clientsocket.send(bytes(str("hello my dudes"),"utf-8"))
      
  
    c1 = clients.pop()
    #c1_addr_port= c1
    #c2 = clients.pop()
    #c1_addr_port = c2

    text = """hello my dudes"""

    content_length = len(text.encode("utf-8"))
    response = RESPONSE.format(length=content_length, body=text)
    c1.send(response.encode("utf-8"))
   # c2.send(response.encode("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)
0

There are 0 best solutions below