[μ 보] μ€νλ§λΆνΈμ νμ΄μ¬μ μ΄μ©νμ¬ JSON ν΅μ νκΈ°
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 νμμΌλ‘ λ°νν©λλ€. μ΄ κ²°κ³Όλ₯Ό μ€νλ§λΆνΈλ‘ λ€μ μ μ‘νμ¬, κ²°κ³Όλ₯Ό νμΈν μ μμ΅λλ€.