Sending a metric via TCP

import socket
conn = socket.create_connection(("carbon.hostedgraphite.com", 2003))
conn.send("YOUR-API-KEY.foo 1\n".encode('utf-8'))
conn.close()

  

Sending a metric via UDP

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto("YOUR-API-KEY.foo 1\n".encode('utf-8'), ("carbon.hostedgraphite.com", 2003))

   

Sending a metric using HTTP POST

With the requests module:

import requests
response = requests.put("http://www.hostedgraphite.com/api/v1/sink", auth = ("YOUR-API-KEY", ""),
data = "foo 1")

   

Using only stdlib modules: (Contributed by Waldo, thanks!)

import urllib.request
from base64 import b64encode

url = "https://www.hostedgraphite.com/api/v1/sink"
api_key = b'YOUR-API-KEY'
metric = "foo 1.2".encode('utf-8')
headers = {'Authorization': b'Basic ' + b64encode(api_key)}
request = urllib.request.Request(url, metric, headers)
result = urllib.request.urlopen(request)

   

Sending a metric via TCP over TLS tunnel

import socket, ssl

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(s)
ssl_sock.connect(("carbon.hostedgraphite.com", 20030))
ssl_sock.write("YOUR-API-KEY.foo 1.2\n".encode("UTF-8"))

    

Your API key can be found on your account home page.