πŸžν”„λ‘œκ·Έλž˜λ°

[정보] μŠ€ν”„λ§λΆ€νŠΈμ™€ νŒŒμ΄μ¬μ„ μ΄μš©ν•˜μ—¬ JSON ν†΅μ‹ ν•˜κΈ°

TwoIceFish 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 ν˜•μ‹μœΌλ‘œ λ°˜ν™˜ν•©λ‹ˆλ‹€. μ΄ κ²°κ³Όλ₯Ό μŠ€ν”„λ§λΆ€νŠΈλ‘œ λ‹€μ‹œ μ „μ†‘ν•˜μ—¬, κ²°κ³Όλ₯Ό ν™•μΈν•  μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.