<p>You might find it easier to just query an electrum server directly. Stratum is a nice protocol in that it's easy to use in an ad-hoc fashion.</p>

<p>If you're using python3 this is almost certainly the easier solution, too.</p>

<pre><code>def get_from_electrum(method, params=[]):
    params = [params] if type(params) is not list else params
    s = socket.create_connection(('ecdsa.net', 50001))
    s.send(json.dumps({"id": 0, "method": method, "params": params}).encode() + b'\n')
    return json.loads(s.recv(99999)[:-1].decode())

get_from_electrum('blockchain.address.get_balance', '1MaxKayeQg4YhFkzFz4x6NDeeNv1bwKKVA')
</code></pre>

<p>Result:</p>

<pre><code>{
    "result": {
        "unconfirmed": 0,
        "confirmed": 237093847
    },
    "id": 0
}
</code></pre>

<p>Documentation: <a href="https://electrum.orain.org/wiki/Stratum_protocol_specification" rel="nofollow">https://electrum.orain.org/wiki/Stratum_protocol_specification</a></p>

CC BY-SA 3.0
2015-10-05T14:44:38.520Z
This no longer works because `the target machine actively refused the connection` (maybe now it uses another host or port), and also that link is broken.
Nathan Parker
CC BY-SA 3.0
2015-04-19T23:56:09.060Z
2015-04-20T00:05:30.337Z
2015-04-20T00:05:30.337Z
Max Kaye
<p>You can use electrum as a library in your scripts.</p>

<p>There is a <a href="https://github.com/spesmilo/electrum/tree/master/scripts" rel="nofollow">scripts directory</a> in the github repo, with examples to learn from.</p>

CC BY-SA 4.0
2024-03-13T07:03:55.270Z
The URL "https://github.com/spesmilo/electrum/tree/master/scripts" for "scripts directory" does no longer work. I got an error 404 from Github, "The 
master branch of electrum does not contain the path scripts."
ewen-goisot
CC BY-SA 3.0
2015-04-19T17:36:33.853Z
2015-04-19T19:33:57.697Z
2015-04-19T19:33:57.697Z
ThomasV
<p>I want to use my electrum wallet balance in my another python application.
How do I make a call to the getbalance() function?</p>

CC BY-SA 3.0
2015-04-19T09:10:34.967Z
It should be something like `electrum daemon start; electrum getbalance`, but I can't get it to work on my machine.
Nick ODell
<electrum><python>
How can I run an Electrum console command from outside Electrum (in Linux)?
latgarf
# How can I run an Electrum console command from outside Electrum (in Linux)?

(Asked by: username_0 on 2015-04-19)

I want to use my electrum wallet balance in my another python application.
How do I make a call to the getbalance() function?

**Tags:** electrum, python

> **Comments:**
> **username_1**: It should be something like `electrum daemon start; electrum getbalance`, but I can't get it to work on my machine.  

---

## 2 answers

### Answer by username_2 on 2015-04-19. Score: 4

You might find it easier to just query an electrum server directly. Stratum is a nice protocol in that it's easy to use in an ad-hoc fashion.

If you're using python3 this is almost certainly the easier solution, too.

```
def get_from_electrum(method, params=[]):
    params = [params] if type(params) is not list else params
    s = socket.create_connection(('ecdsa.net', 50001))
    s.send(json.dumps({"id": 0, "method": method, "params": params}).encode() + b'\n')
    return json.loads(s.recv(99999)[:-1].decode())

get_from_electrum('blockchain.address.get_balance', '1MaxKayeQg4YhFkzFz4x6NDeeNv1bwKKVA')

```

Result:

```
{
    "result": {
        "unconfirmed": 0,
        "confirmed": 237093847
    },
    "id": 0
}

```

Documentation: <https://electrum.orain.org/wiki/Stratum_protocol_specification>

> **Comments:**
> **username_3**: This no longer works because `the target machine actively refused the connection` (maybe now it uses another host or port), and also that link is broken.  

---

### Answer by username_4 on 2015-04-19. Score: 1

You can use electrum as a library in your scripts.

There is a [scripts directory](https://github.com/spesmilo/electrum/tree/master/scripts) in the github repo, with examples to learn from.

> **Comments:**
> **username_5**: The URL "https://github.com/spesmilo/electrum/tree/master/scripts" for "scripts directory" does no longer work. I got an error 404 from Github, "The  master branch of electrum does not contain the path scripts."  

---

