0

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)
Ethan
  • 881
  • 8
  • 14
  • 26
  • 2
    `data = clientsocket.recv(1024)` This is assuming that the browser's initial request fits inside a single packet. It might not - it might choose to send multiple packets. `recv(1024)` is not guaranteed to receive 1024 bytes of data, just at most 1024 bytes of data. You need to call `recv()` in a loop and use the double `\r\n` to know when the browser is done. – Nick ODell Aug 14 '22 at 19:02
  • It might for learning sockets, but why not using a higher level client like `requests`? – Sami Tahri Aug 14 '22 at 19:09
  • I'm using requests for my game hosting for client and for the server just ignore it its just to get server ip . and my game running on Godot. I just gave an example code in python but works the exact same thing in Godot. – alex hernandez Aug 14 '22 at 19:15
  • I'm using requests just to make things easier I think for client side . – alex hernandez Aug 14 '22 at 19:16
  • 1
    https://stackoverflow.com/a/43420503/238704 – President James K. Polk Aug 14 '22 at 22:44
  • one way to fix this is to check if server and client have checked if it went through also the client side could only use requests it doesn't run on socket based . – alex hernandez Aug 14 '22 at 22:52
  • I figured out the issue I just reopen the recv(1042) – alex hernandez Aug 14 '22 at 23:46

0 Answers0