[์ •๋ณด] ์Šคํ”„๋ง๋ถ€ํŠธ์™€ ํŒŒ์ด์ฌ์„ ์ด์šฉํ•˜์—ฌ JSON ํ†ต์‹ ํ•˜๊ธฐ

2023. 2. 17. 13:45ยท๐Ÿžํ”„๋กœ๊ทธ๋ž˜๋ฐ

SON ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ํŒŒ์ด์ฌ์—๊ฒŒ ๋ช…๋ น์–ด๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์€ ๋น„๊ต์  ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ์˜ˆ์ œ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ JSON ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

{
    "command": "run_script",
    "script_path": "/home/user/myscript.py",
    "args": ["arg1", "arg2"],
    "options": {
        "option1": "value1",
        "option2": "value2"
    }
}

์œ„์˜ JSON ๊ตฌ์กฐ์ฒด๋Š” "run_script" ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. "script_path"๋Š” ์‹คํ–‰ํ•  ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ, "args"๋Š” ๋ช…๋ นํ–‰ ์ธ์ž๋ฅผ, "options"๋Š” ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰์— ํ•„์š”ํ•œ ์˜ต์…˜์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ด JSON ๊ตฌ์กฐ์ฒด๋ฅผ ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ํŒŒ์ด์ฌ์œผ๋กœ ์ „์†กํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

// ...

RestTemplate restTemplate = new RestTemplate();

// Set the headers for the HTTP request
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// Set the request body with the JSON structure
String json = "{ \"command\": \"run_script\", \"script_path\": \"/home/user/myscript.py\", \"args\": [\"arg1\", \"arg2\"], \"options\": { \"option1\": \"value1\", \"option2\": \"value2\" } }";
HttpEntity<String> entity = new HttpEntity<String>(json, headers);

// Send the HTTP request to the Python server
String url = "http://localhost:5000/run_command";
String response = restTemplate.postForObject(url, entity, String.class);

// Process the response from the Python server
// ...

์œ„์˜ ์ฝ”๋“œ์—์„œ "http://localhost:5000/run_command"๋Š” ํŒŒ์ด์ฌ ์„œ๋ฒ„์˜ ์—”๋“œํฌ์ธํŠธ URL์ž…๋‹ˆ๋‹ค. ์ด์ œ ํŒŒ์ด์ฌ์—์„œ ์ด ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

import json

def process_command(command):
    if command["command"] == "run_script":
        script_path = command["script_path"]
        args = command["args"]
        options = command["options"]
        # Run the script with the given arguments and options
        # ...
    else:
        # Handle unrecognized command
        # ...

# ...

# Receive the JSON command from the Spring Boot server
# ...
json_command = '{"command": "run_script", "script_path": "/home/user/myscript.py", "args": ["arg1", "arg2"], "options": { "option1": "value1", "option2": "value2" } }'

# Parse the JSON command into a dictionary
command = json.loads(json_command)

# Process the command
process_command(command)

์œ„์˜ ์ฝ”๋“œ์—์„œ "process_command" ํ•จ์ˆ˜๋Š” ๋ฐ›์€ JSON ๋ช…๋ น์–ด์— ๋Œ€ํ•œ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. "if" ๋ฌธ์—์„œ "command" ํ•„๋“œ๋ฅผ ํ™•์ธํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ํŒŒ์ด์ฌ์œผ๋กœ JSON ํ˜•์‹์œผ๋กœ ์ „์†กํ•œ ๋ช…๋ น์–ด๋ฅผ ํŒŒ์ด์ฌ์—์„œ ์ˆ˜์‹ ํ•˜๊ณ , "json.loads" ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ "process_command" ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. "process_command" ํ•จ์ˆ˜์—์„œ๋Š” "if" ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ "command" ํ•„๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ , ํ•ด๋‹นํ•˜๋Š” ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "run_script" ๋ช…๋ น์ด ์ „์†ก๋œ ๊ฒฝ์šฐ, "script_path", "args", "options" ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒํ•˜๋ฉด ์Šคํ”„๋ง๋ถ€ํŠธ์™€ ํŒŒ์ด์ฌ ๊ฐ„์— JSON ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…๋ น์–ด๋ฅผ ์ „์†กํ•˜๊ณ , ํŒŒ์ด์ฌ์—์„œ ํ•ด๋‹น ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๋ณต์žกํ•œ ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

JSON์—์„œ ์‚ฌ์šฉํ•  "command" ํ•„๋“œ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํ‚ค์›Œ๋“œ๋Š” ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ง€์›ํ•˜๋Š” ๋ช…๋ น์–ด์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์‹œ์Šคํ…œ ๊ด€๋ฆฌ ๋ฐ ์ž๋™ํ™”๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ‚ค์›Œ๋“œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

  • start: ์„œ๋น„์Šค๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • stop: ์„œ๋น„์Šค๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.
  • restart: ์„œ๋น„์Šค๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์žฌ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • status: ์„œ๋น„์Šค๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • configure: ์„ค์ • ํŒŒ์ผ์ด๋‚˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋“ฑ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • deploy: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฐฐํฌํ•ฉ๋‹ˆ๋‹ค.

์œ„์˜ ์˜ˆ์‹œ๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ช…๋ น์–ด์ด๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋”ฐ๋ผ ์ง€์›ํ•˜๋Š” ๋ช…๋ น์–ด์˜ ์ข…๋ฅ˜๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์— ๋งž๊ฒŒ "command" ํ•„๋“œ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์ •์˜ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์•ž์„œ ์˜ˆ์‹œ๋กœ ๋“ค์–ด์ค€ "run_script" ๋ช…๋ น์„ ํฌํ•จํ•˜์—ฌ, ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ๋Š” "/run_command" ์—”๋“œํฌ์ธํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํŒŒ์ด์ฌ์œผ๋กœ๋ถ€ํ„ฐ ์ „์†ก๋œ JSON ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , ํŒŒ์ด์ฌ์—์„œ๋Š” ์ด๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

์Šคํ”„๋ง๋ถ€ํŠธ ์ฝ”๋“œ:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CommandController {
    @PostMapping("/run_command")
    public String runCommand(@RequestBody String commandJson) {
        RestTemplate restTemplate = new RestTemplate();

        // Set the headers for the HTTP request
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // Set the request body with the JSON structure
        HttpEntity<String> entity = new HttpEntity<String>(commandJson, headers);

        // Send the HTTP request to the Python server
        String url = "http://localhost:5000/run_command";
        String response = restTemplate.postForObject(url, entity, String.class);

        return response;
    }
}

ํŒŒ์ด์ฌ ์ฝ”๋“œ:

import json

def process_command(command):
    if command["command"] == "run_script":
        script_path = command["script_path"]
        args = command["args"]
        options = command["options"]
        # Run the script with the given arguments and options
        # ...
        return "Script executed successfully"
    elif command["command"] == "status":
        # Check the status of the application and return it
        status = "running"
        return status
    elif command["command"] == "stop":
        # Stop the application and return the status
        status = "stopped"
        return status
    else:
        # Handle unrecognized command
        return "Unrecognized command"

# ...

# Receive the JSON command from the Spring Boot server
# ...
json_command = '{"command": "run_script", "script_path": "/home/user/myscript.py", "args": ["arg1", "arg2"], "options": { "option1": "value1", "option2": "value2" } }'

# Parse the JSON command into a dictionary
command = json.loads(json_command)

# Process the command
response = process_command(command)

# Send the response back to the Spring Boot server
# ...
json_response = json.dumps({"response": response})

 

์œ„์˜ ์ฝ”๋“œ์—์„œ "/run_command" ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ†ตํ•ด ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ํŒŒ์ด์ฌ์œผ๋กœ ์ „์†ก๋˜๋Š” JSON ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” "process_command" ํ•จ์ˆ˜์—์„œ "command" ํ•„๋“œ๋ฅผ ํ™•์ธํ•˜์—ฌ ํ•ด๋‹น ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ฒฐ๊ณผ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฐ๊ณผ๋ฅผ ์Šคํ”„๋ง๋ถ€ํŠธ๋กœ ๋‹ค์‹œ ์ „์†กํ•˜์—ฌ, ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐Ÿžํ”„๋กœ๊ทธ๋ž˜๋ฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[์ž๋ฐ”] OTP ์ธ์ฆ์ฝ”๋“œ ์ƒ์„ฑ ์†Œ์Šค์ฝ”๋“œ  (0) 2023.02.21
[์ •๋ณด] ํŒŒ์ด์ฌ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์—ฌ๋Ÿฌ๊ฐœ ํ•˜๊ธฐ  (0) 2023.02.17
[์ •๋ณด] ํŒŒ์ด์ฌ ๋„คํŠธ์›Œํฌ ํ†ต์‹  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ข…๋ฅ˜ ๋ฐ ์˜ˆ์ œ  (0) 2023.02.15
[์ •๋ณด] ํŒŒ์ด์ฌ subprocess ์„ค๋ช… ๋ฐ ์˜ˆ์ œ  (0) 2023.02.15
[์ •๋ณด] JavaScript์—์„œ HTTP ์š”์ฒญ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•  (0) 2023.02.15
'๐Ÿžํ”„๋กœ๊ทธ๋ž˜๋ฐ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [์ž๋ฐ”] OTP ์ธ์ฆ์ฝ”๋“œ ์ƒ์„ฑ ์†Œ์Šค์ฝ”๋“œ
  • [์ •๋ณด] ํŒŒ์ด์ฌ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์—ฌ๋Ÿฌ๊ฐœ ํ•˜๊ธฐ
  • [์ •๋ณด] ํŒŒ์ด์ฌ ๋„คํŠธ์›Œํฌ ํ†ต์‹  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ข…๋ฅ˜ ๋ฐ ์˜ˆ์ œ
  • [์ •๋ณด] ํŒŒ์ด์ฌ subprocess ์„ค๋ช… ๋ฐ ์˜ˆ์ œ
TwoIceFish
TwoIceFish
https://github.com/TwoIceFIsh
  • TwoIceFish
    Cyber-Luna
    TwoIceFish
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (593)
      • ๐Ÿค–์ •๋ณด๋ณด์•ˆ (77)
        • ๐Ÿ’™๋ธ”๋ฃจํŒ€ (24)
        • โค๏ธ๋ ˆ๋“œํŒ€ (21)
        • ๐Ÿ’œํผํ”ŒํŒ€ (1)
        • ๐Ÿ’ 1๋ถ„์ง€์‹ (30)
      • ํ”„๋กœ์ ํŠธ (14)
        • ๐Ÿ’Œ ์ •๋ณด๋ณด์•ˆ ๋ฉ”์ผ๋ง ์‹œ์Šคํ…œ (8)
        • ๐Ÿ” ์ธ์ฆ์„œ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ (1)
        • ๐Ÿ ๊ธˆ์œต ์ปค๋ฎค๋‹ˆํ‹ฐ (5)
      • ๐Ÿžํ”„๋กœ๊ทธ๋ž˜๋ฐ (49)
        • Next.js (9)
      • ๊ธฐํƒ€์ •๋ณด (68)
        • ๐ŸŒ๊ทธ๋ฆฟ์š”๊ฑฐํŠธ (11)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ๋ฐฉ๋ช…๋ก
    • ๋กœ์ผ“ํŽ€์น˜
    • ๊นƒํ—ˆ๋ธŒ
    • ์ฝ”์ฝ”๋„ˆ์ธ 
    • ๊ทธ๋ฆฟ์š”๊ฑฐํŠธ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

    • ์•ˆ๋…•ํ•˜์„ธ์š”
  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ์‚ผ์„ฑ์ „์ž์šฐ
    ๋ถ€๋™์‚ฐ ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ๊ธฐ
    jsp 200
    ๋ฉ”์ผํ—ค๋”๋ถ„์„
    ์ธ์ฆ์„œ ์—ฌ๋Ÿฌ๊ฐœ
    nmap
    ๋ฐฉ๋ฒ™
    servlet 404
    vpn ์˜คํ”ˆ์†Œ์Šค
    SKํ•˜์ด๋‹‰์Šค
    ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ๊ธฐ
    ๋‹จ์ผ ๋„๋ฉ”์ธ ์ธ์ฆ์„œ ์—ฌ๋Ÿฌ๊ฐœ
    ์•…์„ฑ๋ฉ”์ผ
    ์• ํ”ŒํŽ˜์ด ์„ค์ •๋ฐฉ๋ฒ•
    ์ง€๊ฐ‘ ์•ฑ์— ์นด๋“œ ์ถ”๊ฐ€
    ํ†ฐ์บฃ ์„œ๋ธ”๋ฆฟ
    Visual Studio
    tomcat servlet
    ์ฝ”์ฝ”๋„›์ธ 
    ์•…์„ฑ๋ฉ”์žƒ๋ถ„์„
    eclipse
    ์‘๋‹ต์—†์Œ
    ์™€์ดํŒŒ์ด ๋น„๋ฐ€๋ฒˆํ˜ธ ํƒˆ์ทจ
    ๋ถ€ํŠธ์ŠคํŠธ๋žฉ
    ์„œ๋ธŒ๋„๋ฉ”์ธ ์ธ์ฆ์„œ
    ISMS-P
    ๋ชจ์˜ํ•ดํ‚น
    vpn ์„ค์น˜
    jsp
    ์œ ๋‹ˆํ‹ฐ
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
TwoIceFish
[์ •๋ณด] ์Šคํ”„๋ง๋ถ€ํŠธ์™€ ํŒŒ์ด์ฌ์„ ์ด์šฉํ•˜์—ฌ JSON ํ†ต์‹ ํ•˜๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”