[μ 보] νμ΄μ¬ λ€νΈμν¬ ν΅μ νλ‘κ·Έλλ° μ’ λ₯ λ° μμ
νμ΄μ¬μμ λ€λ₯Έ μ격μ§μ μλ νλ‘κ·Έλ¨κ³Ό ν΅μ νλ λ°©λ²μ μ¬λ¬ κ°μ§κ° μμ΅λλ€. λ€μμ λνμ μΈ λ°©λ² μ€ μΌλΆμ κ°λ¨ν μμ μ½λμ λλ€.
1. HTTP(S) νλ‘ν μ½μ μ΄μ©ν μΉ μλΉμ€ νμ΄μ¬μμλ requests λΌμ΄λΈλ¬λ¦¬λ₯Ό μ΄μ©νμ¬ HTTP(S) μμ²μ 보λ΄κ³ λ°μ μ μμ΅λλ€.
import requests
url = 'https://example.com/api/v1'
headers = {'Content-Type': 'application/json'}
data = {'param1': 'value1', 'param2': 'value2'}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
print(response.json())
else:
print('Request failed with status code:', response.status_code)
2. TCP/IP μμΌ νλ‘κ·Έλλ° Pythonμμλ socket λͺ¨λμ μ¬μ©νμ¬ TCP/IP μμΌ νλ‘κ·Έλλ°μ ν μ μμ΅λλ€.
import socket
host = 'remote_host.com'
port = 1234
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received:', data)
3. λ©μμ§ ν Pythonμμλ pika λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©νμ¬ RabbitMQμ κ°μ λ©μμ§ ν μμ€ν κ³Ό ν΅μ ν μ μμ΅λλ€.
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
queue_name = 'my_queue'
message = 'Hello, world'
channel.queue_declare(queue=queue_name)
channel.basic_publish(exchange='', routing_key=queue_name, body=message)
print("Sent message:", message)
def callback(ch, method, properties, body):
print("Received message:", body.decode())
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()
μ μμ μ½λλ κ° λ°©λ²μ κΈ°λ³Έμ μΈ μμ μ½λμΌ λΏ, μν©μ λ°λΌ λ λ€μν νλΌλ―Έν°λ κΈ°λ₯μ μ¬μ©ν μ μμ΅λλ€. μ΄μ λ°λΌ λ 볡μ‘ν μ½λκ° νμν μ μμ΅λλ€.