0

I have a script to get json and run it on my ubuntu server with a white ip I accept json, but not completely, and after this acceptance, the script closes the connection and does not work I think the problem is that I receive packets incorrectly, but why it does not accept the second packet and why it closes in an infinite loop is not clear to me due to my little experience

import os
import socket
from pathlib import Path
from dotenv import load_dotenv
import json


#Init .env
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
#create webserver socket
def start_my_server():
    socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_server.bind(('ip', port))
    socket_server.listen(32768)
    print('Working...')
    global data
    global HDRS
    while True:
        client_socket, address = socket_server.accept()
        data = client_socket.recv(32768).decode('utf-8')
        # content = 'Well done'.encode('utf-8')
        HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
        # content = load_page_from_get_request(data)
        client_socket.send(HDRS.encode('utf-8'))
        # a = client_socket.send(HDRS.encode('utf-8'))
        # print(a, '+'*20)
        client_socket.shutdown(socket.SHUT_WR)
        load_page_from_get_request(32768)
    # print('end')
    # socket_server.close()

def load_page_from_get_request(request_data):
    HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
    HDRS_404 = 'HTTP/1.1 404 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
    try:
        with open('data.json', 'w') as output_file:
            json.dump(data, output_file)
        return HDRS.encode('utf-8')
    except EOFError:
        return HDRS_404.encode('utf-8')
    # try

if __name__ == '__main__':
    start_my_server()
Miekrif
  • 55
  • 7
  • the argument to `client_socket.recv` should be the number of bytes to receive, NOT the port. Try using [Websockets](https://websockets.readthedocs.io/en/stable/), I think it will be a better fit for your use case. – Jack Mar 05 '22 at 09:58
  • I know this, it is bytes For link thx, i write this and give callback for – Miekrif Mar 05 '22 at 10:23
  • sorry, i misread your code, I thought you used that same value for `server_socket.bind` – Jack Mar 05 '22 at 10:30
  • 1) This doesn't appear to be HTTP 1.1 compliant; 2) your receive loop is very brittle because it relies on incorrect assumptions about `socket.recv()`, see [this answer](https://stackoverflow.com/a/43420503/238704); 3) `load_page_from_get_request(32768)` doesn't make any sense to me, I think you wanted to call `load_page_from_get_request(data)` – President James K. Polk Mar 05 '22 at 14:00
  • thx mr President, i end my script – Miekrif Mar 06 '22 at 21:13

1 Answers1

0

The end version of my script

import os
import socket
from pathlib import Path
from dotenv import load_dotenv
import json
import datetime


#Init .env
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
ip = os.environ['ip']
port = os.environ['port']

HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
HDRS_404 = 'HTTP/1.1 404 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
#create webserver socket
def start_my_server():
    socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_server.bind((ip, port))
    socket_server.listen(32768)
    print('Working...')
    global data
    while True:
        client_socket, address = socket_server.accept()
        print('loop', address)
        data = client_socket.recv(0).decode('utf-8')
        # content = load_page_from_get_request(data)
        load_page_from_get_request(data)
        client_socket.send(HDRS.encode('utf-8'))
        client_socket.shutdown(socket.SHUT_WR)
    # print('end')
    # socket_server.close()

def load_page_from_get_request(request_data):
    # HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
    # HDRS_404 = 'HTTP/1.1 404 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
    with open(f'data.json{datetime.time}', 'wr') as output_file:
            json.dump(data, output_file)
    return HDRS.encode('utf-8')
    # except EOFError:
    #     return HDRS_404.encode('utf-8')
    # try

print('loop script')
if __name__ == '__main__':
    start_my_server()
Miekrif
  • 55
  • 7