# Python 2.x

### Interesting Python Graphite Libraries

* **A Python module for sending metrics to Graphite over UDP**\
  <https://github.com/derpston/python-graphiteudp> - Charlie wrote this library, and we use it internally. It’s a good simple library for sending metrics over UDP in Python.
* **Diamond - Send system metrics to Graphite**\
  [*Diamond*](/agents-guide/diamond.md) - A really useful Python daemon to send system metrics such as CPU usage, load, Disk IO, etc.

### Sending a metric via TCP

```
import socket

conn = socket.create_connection(("YOUR-UID.carbon.hostedgraphite.com", 2003))
conn.send("YOUR-API-KEY.foo.py-tcp 1.2\n")
conn.close()
```

### Sending a metric via UDP

```
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto("YOUR-API-KEY.foo.py-udp 1.2\n", ("YOUR-UID.carbon.hostedgraphite.com", 2003))
```

### Sending a metric using HTTP POST

```
import urllib2, base64

url = "https://www.hostedgraphite.com/api/v1/sink"
api_key = "YOUR-API-KEY"

request = urllib2.Request(url, "foo.py-http 1.2")
request.add_header("Authorization", "Basic %s" % base64.encodestring(api_key).strip())
result = urllib2.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(("YOUR-UID.carbon.hostedgraphite.com", 20030))
ssl_sock.write("YOUR-API-KEY.foo.py-tls 1.2\n")
```

Your API key can be found on your [account home](https://www.hostedgraphite.com/accounts/profile/) page.

## Python 3.x <a href="#python-3.x" id="python-3.x"></a>

### Sending a metric via TCP

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

### Sending a metric via UDP

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

### Sending a metric using HTTP POST

With the requests module:

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

Using only stdlib modules: (Contributed by [Waldo](https://github.com/gwaldo/HostedGraphite_Python3), thanks!)

```python
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

```python
import socket, ssl

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

### Sending a metric via StatsD

```python
import socket, ssl

import socket

statsd_host = 'statsd.hostedgraphite.com'
statsd_port = 8125
metric_key = 'YOUR-API-KEY.foo.py3-statsd'
metric_value = 1.2

metric_message = f"{metric_key}:{metric_value}|c"

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(metric_message.encode('utf-8'), (statsd_host, statsd_port))
sock.close()
```

When sending StatsD metrics, make sure to specify counters (c), gauges (g), or timers (ms).

Your API key can be found on your [account home](https://www.hostedgraphite.com/accounts/profile/) page.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hostedgraphite.com/language-guide/python-2.x.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
