create account

Execute python code directly from a steem post by holger80

View this thread on: hive.blogpeakd.comecency.com
· @holger80 · (edited)
$41.29
Execute python code directly from a steem post
It is possible to execute python code directly from a post. I took my last [post](https://hive.blog/steemdev/@holger80/how-to-write-all-decrypted-memos-into-a-csv-file)  and stored the included python code in a post from @beempy: https://hive.blog/python/@beempy/decrypt-memos-csv

It is now possible to run this code without storing it on a file:

```
#!/usr/bin/python
from beem import Steem
from beem.comment import Comment
import argparse


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("authorperm")
    parser.add_argument("timestamp", nargs='?', default=None)
    args = parser.parse_args()
    authorperm = args.authorperm
    timestamp = args.timestamp
    stm = Steem(node="https://api.steemit.com")
    comment = Comment(authorperm, steem_instance=stm)
    if timestamp is not None and comment.json()["last_update"] != timestamp:
        print("%s != %s, aborting" % (comment.json()["last_update"], timestamp))
    else:
        start_position = comment.body.find("```") + 4
        end_position = comment.body.find("```", 2)
        python_file = comment.body[start_position:end_position]
        exec(python_file, globals())
    
```

This simple script reads the post and searches the position of the included python code. Then, the code is executed. Store the lines as python script `read_python_from_post.py` and execute it by

```
python read_python_from_post.py @beempy/decrypt-memos-csv
```

![](https://cdn.steemitimages.com/DQmdk5jqEXFfERZ2GAQesS5D19A8njiiy7b6oEMcg3VcGax/image.png)

The output and behavior are the same as when the script would be stored directly in the python file.

I added more security by checking the `last_update` timestamp. When the given timestamp is different from the comment parameter, the code is not executed:
```
python read_python_from_post.py @beempy/decrypt-memos-csv 2018-11-22T23:11:18
```
This prevents the execution of malicious code when the posting key of @beempy would have been leaked and someone had edited the posts.

It is also possible to store all python scripts that were published by @beempy:

```
#!/usr/bin/python
from beem import Steem
from beem.comment import Comment
from beem.account import Account
import os


if __name__ == "__main__":
    output_file_path = "."
    stm = Steem(node="https://api.steemit.com")
    account = Account("beempy", steem_instance=stm)
    blog_posts = account.get_blog()
    index = 0
    for comment in blog_posts:
        print("writing %s.py" % comment["permlink"])
        start_position = comment.body.find("```") + 4
        end_position = comment.body.find("```", 2)
        python_file = comment.body[start_position:end_position]
        file_name = os.path.join(output_file_path, comment["permlink"] + '_' + comment.json()["last_update"] + ".py")
        with open(file_name, "w") as f:
            f.write(python_file)
   
     
```

At the moment, only one script is published, but I plan to add more content to @beempy.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 88 others
properties (23)
authorholger80
permlinkexecute-python-code-directly-from-a-steem-post
categorysteemdev
json_metadata"{"community": "beem", "app": "beempy/0.23.10", "users": ["beempy"], "image": ["https://cdn.steemitimages.com/DQmdk5jqEXFfERZ2GAQesS5D19A8njiiy7b6oEMcg3VcGax/image.png"], "links": ["https://hive.blog/steemdev/@holger80/how-to-write-all-decrypted-memos-into-a-csv-file", "https://hive.blog/python/@beempy/decrypt-memos-csv"], "tags": ["steemdev", "steemtank", "python", "beem"], "format": "markdown", "canonical_url": "https://hive.blog/steemdev/@holger80/execute-python-code-directly-from-a-steem-post"}"
created2018-11-23 00:04:51
last_update2020-05-23 14:43:45
depth0
children14
last_payout2018-11-30 00:04:51
cashout_time1969-12-31 23:59:59
total_payout_value31.689 HBD
curator_payout_value9.602 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,982
author_reputation358,857,509,568,825
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,758,630
net_rshares68,757,718,893,241
author_curate_reward""
vote details (152)
@geekpowered ·
$0.03
Um...that seems potentially very insecure...
👍  ,
properties (23)
authorgeekpowered
permlinkre-holger80-execute-python-code-directly-from-a-steem-post-20181123t053938721z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 05:39:39
last_update2018-11-23 05:39:39
depth1
children2
last_payout2018-11-30 05:39:39
cashout_time1969-12-31 23:59:59
total_payout_value0.023 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length44
author_reputation99,319,261,112,097
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,769,249
net_rshares50,714,683,725
author_curate_reward""
vote details (2)
@holger80 · (edited)
You are right and I added the possibility to add the post timestamp as security check.
By this, it is guaranteed that nobody else edited the code and that the source code itself did not change.
properties (22)
authorholger80
permlinkre-geekpowered-re-holger80-execute-python-code-directly-from-a-steem-post-20181123t065739424z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 06:57:42
last_update2018-11-23 06:58:06
depth2
children0
last_payout2018-11-30 06:57:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length193
author_reputation358,857,509,568,825
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,771,634
net_rshares0
@personz ·
My thoughts exactly
properties (22)
authorpersonz
permlinkre-geekpowered-re-holger80-execute-python-code-directly-from-a-steem-post-20181123t085558278z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 08:56:00
last_update2018-11-23 08:56:00
depth2
children0
last_payout2018-11-30 08:56:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length19
author_reputation42,452,361,038,560
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,775,354
net_rshares0
@holger80 ·
```
#!/usr/bin/python
from beem import Steem
from beem.comment import Comment
import getpass


if __name__ == "__main__":

    wif = getpass.getpass(prompt='Enter your posting key.')
    stm = Steem(keys=[wif])
    account = stm.wallet.getAccountFromPrivateKey(wif)
    comment = Comment("@holger80/execute-python-code-directly-from-a-steem-post")
    print("%s will upvote %s" % (account, comment["permlink"]))
    comment.upvote(voter=account)
    
```
properties (22)
authorholger80
permlinkre-holger80-execute-python-code-directly-from-a-steem-post-20181123t003450162z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 00:34:51
last_update2018-11-23 00:34:51
depth1
children1
last_payout2018-11-30 00:34:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length454
author_reputation358,857,509,568,825
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,759,693
net_rshares0
@holger80 · (edited)
You can upvote my post after storing the `read_python_from_post` script from the post by:
```
python read_python_from_post.py @holger80/re-holger80-execute-python-code-directly-from-a-steem-post-20181123t003450162z 2018-11-23T00:34:51
```
properties (22)
authorholger80
permlinkre-holger80-re-holger80-execute-python-code-directly-from-a-steem-post-20181123t003943096z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 00:39:45
last_update2018-11-23 06:56:09
depth2
children0
last_payout2018-11-30 00:39:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length238
author_reputation358,857,509,568,825
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,759,838
net_rshares0
@ocdb ·
re-holger80-execute-python-code-directly-from-a-steem-post-20181123t075712014z
You got a 54.88% upvote from @ocdb courtesy of @holger80!

@ocdb is a non-profit bidbot for whitelisted Steemians, current max bid is 20 SBD and the respective amount in Steem.
Check our website https://thegoodwhales.io/ for the whitelist, queue and delegation info. Join our [Discord channel for more information.](https://discord.gg/k2Hu77b)

If you like what @ocd does, consider voting for [ocd-witness through SteemConnect](steemconnect.com/sign/account-witness-vote?witness=ocd-witness&approve=1) or on the Steemit Witnesses page. :)
properties (22)
authorocdb
permlinkre-holger80-execute-python-code-directly-from-a-steem-post-20181123t075712014z
categorysteemdev
json_metadata{"app":"postpromoter/1.9.3"}
created2018-11-23 07:57:12
last_update2018-11-23 07:57:12
depth1
children0
last_payout2018-11-30 07:57:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length538
author_reputation762,966,985,103,066
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,773,446
net_rshares0
@schlafhacking ·
$0.03
eval(someshit) is evil.
eval(somshitstoredinablockchain) is still evil.
Funny idea though ;-)
👍  
properties (23)
authorschlafhacking
permlinkre-holger80-execute-python-code-directly-from-a-steem-post-20181123t095144869z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 09:51:45
last_update2018-11-23 09:51:45
depth1
children1
last_payout2018-11-30 09:51:45
cashout_time1969-12-31 23:59:59
total_payout_value0.019 HBD
curator_payout_value0.006 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length93
author_reputation15,067,517,752,330
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,777,122
net_rshares41,954,039,593
author_curate_reward""
vote details (1)
@holger80 ·
I will look into IronPython and RestrictedPython
properties (22)
authorholger80
permlinkre-schlafhacking-re-holger80-execute-python-code-directly-from-a-steem-post-20181123t115818786z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 11:58:21
last_update2018-11-23 11:58:21
depth2
children0
last_payout2018-11-30 11:58:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length48
author_reputation358,857,509,568,825
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,781,250
net_rshares0
@sneak · (edited)
$0.03
It is probably worth noting that this is fully trusting the `steemd` node to which the Steem client library is connecting—trusting it with full RCE, that is..  It probably shouldn't be run except pointed at a node you run/control/trust, and this script as written does not do that.
👍  
properties (23)
authorsneak
permlinkre-holger80-execute-python-code-directly-from-a-steem-post-20181123t102336287z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 10:23:36
last_update2018-11-23 10:23:54
depth1
children1
last_payout2018-11-30 10:23:36
cashout_time1969-12-31 23:59:59
total_payout_value0.019 HBD
curator_payout_value0.006 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length281
author_reputation28,694,344,106,492
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,778,162
net_rshares42,080,115,774
author_curate_reward""
vote details (1)
@holger80 ·
Good point, I added a line in which a node is specified. I'm using now the api.steemit.com node, which should be trustworthy.
properties (22)
authorholger80
permlinkre-sneak-re-holger80-execute-python-code-directly-from-a-steem-post-20181123t115726033z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-11-23 11:57:27
last_update2018-11-23 11:57:27
depth2
children0
last_payout2018-11-30 11:57:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length125
author_reputation358,857,509,568,825
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,781,219
net_rshares0
@steem-plus ·
SteemPlus upvote
Hi, @holger80!

You just got a **2.27%** upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in [here](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
authorsteem-plus
permlinkexecute-python-code-directly-from-a-steem-post---vote-steemplus
categorysteemdev
json_metadata{}
created2018-11-23 02:02:36
last_update2018-11-23 02:02:36
depth1
children0
last_payout2018-11-30 02:02:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length435
author_reputation247,952,188,232,400
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,762,405
net_rshares0
@steem-ua ·
#### Hi @holger80!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your **UA** account score is currently 7.244 which ranks you at **#69** across all Steem accounts.
Your rank has not changed in the last three days.

In our last Algorithmic Curation Round, consisting of 213 contributions, your post is ranked at **#2**. Congratulations! 
##### Evaluation of your UA score:

* Your follower network is great!
* The readers appreciate your great work!
* Good user engagement!


**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-execute-python-code-directly-from-a-steem-post-20181123t121946z
categorysteemdev
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-23 12:19:48
last_update2018-11-23 12:19:48
depth1
children0
last_payout2018-11-30 12:19:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length613
author_reputation23,214,230,978,060
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,781,955
net_rshares0
@steemprojects ·
This post has been just added as new item to _[timeline of beem on Steem Projects](https://steemprojects.com/projects/p/beem/?utm_source=comment_timeline&utm_medium=steem&utm_campaign=new_event&utm_content=c1)_.

If you want to be notified about new updates from this project, register on Steem Projects and add beem to your favorite projects.
properties (22)
authorsteemprojects
permlinkre-execute-python-code-directly-from-a-steem-post-20181123t001929
categorysteemdev
json_metadata""
created2018-11-23 00:19:30
last_update2018-11-23 00:19:30
depth1
children0
last_payout2018-11-30 00:19:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length345
author_reputation29,054,729,340
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,759,149
net_rshares0
@tts ·
To listen to the audio version of this article click on the play image.
[![](https://s18.postimg.org/51o0kpijd/play200x46.png)](http://ec2-52-72-169-104.compute-1.amazonaws.com/holger80__execute-python-code-directly-from-a-steem-post.mp3)
Brought to you by [@tts](https://steemit.com/tts/@tts/introduction). If you find it useful please consider upvoting this reply.
properties (22)
authortts
permlinkre-execute-python-code-directly-from-a-steem-post-20181123t002114
categorysteemdev
json_metadata""
created2018-11-23 00:21:15
last_update2018-11-23 00:21:15
depth1
children0
last_payout2018-11-30 00:21:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length366
author_reputation-4,535,154,553,995
root_title"Execute python code directly from a steem post"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,759,219
net_rshares0