Best Python code snippet using locust
bitrpc.py
Source:bitrpc.py  
1from jsonrpc import ServiceProxy2import sys3import string4# ===== BEGIN USER SETTINGS =====5# if you do not set these you will be prompted for a password for every command6rpcuser = ""7rpcpass = ""8# ====== END USER SETTINGS ======9if rpcpass == "":10	access = ServiceProxy("http://127.0.0.1:8332")11else:12	access = ServiceProxy("http://"+rpcuser+":"+rpcpass+"@127.0.0.1:8332")13cmd = sys.argv[1].lower()14if cmd == "backupwallet":15	try:16		path = raw_input("Enter destination path/filename: ")17		print access.backupwallet(path)18	except:19		print "\n---An error occurred---\n"20elif cmd == "getaccount":21	try:22		addr = raw_input("Enter a Bitcoin address: ")23		print access.getaccount(addr)24	except:25		print "\n---An error occurred---\n"26elif cmd == "getaccountaddress":27	try:28		acct = raw_input("Enter an account name: ")29		print access.getaccountaddress(acct)30	except:31		print "\n---An error occurred---\n"32elif cmd == "getaddressesbyaccount":33	try:34		acct = raw_input("Enter an account name: ")35		print access.getaddressesbyaccount(acct)36	except:37		print "\n---An error occurred---\n"38elif cmd == "getbalance":39	try:40		acct = raw_input("Enter an account (optional): ")41		mc = raw_input("Minimum confirmations (optional): ")42		try:43			print access.getbalance(acct, mc)44		except:45			print access.getbalance()46	except:47		print "\n---An error occurred---\n"48elif cmd == "getblockbycount":49	try:50		height = raw_input("Height: ")51		print access.getblockbycount(height)52	except:53		print "\n---An error occurred---\n"54elif cmd == "getblockcount":55	try:56		print access.getblockcount()57	except:58		print "\n---An error occurred---\n"59elif cmd == "getblocknumber":60	try:61		print access.getblocknumber()62	except:63		print "\n---An error occurred---\n"64elif cmd == "getconnectioncount":65	try:66		print access.getconnectioncount()67	except:68		print "\n---An error occurred---\n"69elif cmd == "getdifficulty":70	try:71		print access.getdifficulty()72	except:73		print "\n---An error occurred---\n"74elif cmd == "getgenerate":75	try:76		print access.getgenerate()77	except:78		print "\n---An error occurred---\n"79elif cmd == "gethashespersec":80	try:81		print access.gethashespersec()82	except:83		print "\n---An error occurred---\n"84elif cmd == "getinfo":85	try:86		print access.getinfo()87	except:88		print "\n---An error occurred---\n"89elif cmd == "getnewaddress":90	try:91		acct = raw_input("Enter an account name: ")92		try:93			print access.getnewaddress(acct)94		except:95			print access.getnewaddress()96	except:97		print "\n---An error occurred---\n"98elif cmd == "getreceivedbyaccount":99	try:100		acct = raw_input("Enter an account (optional): ")101		mc = raw_input("Minimum confirmations (optional): ")102		try:103			print access.getreceivedbyaccount(acct, mc)104		except:105			print access.getreceivedbyaccount()106	except:107		print "\n---An error occurred---\n"108elif cmd == "getreceivedbyaddress":109	try:110		addr = raw_input("Enter a Bitcoin address (optional): ")111		mc = raw_input("Minimum confirmations (optional): ")112		try:113			print access.getreceivedbyaddress(addr, mc)114		except:115			print access.getreceivedbyaddress()116	except:117		print "\n---An error occurred---\n"118elif cmd == "gettransaction":119	try:120		txid = raw_input("Enter a transaction ID: ")121		print access.gettransaction(txid)122	except:123		print "\n---An error occurred---\n"124elif cmd == "getwork":125	try:126		data = raw_input("Data (optional): ")127		try:128			print access.gettransaction(data)129		except:130			print access.gettransaction()131	except:132		print "\n---An error occurred---\n"133elif cmd == "help":134	try:135		cmd = raw_input("Command (optional): ")136		try:137			print access.help(cmd)138		except:139			print access.help()140	except:141		print "\n---An error occurred---\n"142elif cmd == "listaccounts":143	try:144		mc = raw_input("Minimum confirmations (optional): ")145		try:146			print access.listaccounts(mc)147		except:148			print access.listaccounts()149	except:150		print "\n---An error occurred---\n"151elif cmd == "listreceivedbyaccount":152	try:153		mc = raw_input("Minimum confirmations (optional): ")154		incemp = raw_input("Include empty? (true/false, optional): ")155		try:156			print access.listreceivedbyaccount(mc, incemp)157		except:158			print access.listreceivedbyaccount()159	except:160		print "\n---An error occurred---\n"161elif cmd == "listreceivedbyaddress":162	try:163		mc = raw_input("Minimum confirmations (optional): ")164		incemp = raw_input("Include empty? (true/false, optional): ")165		try:166			print access.listreceivedbyaddress(mc, incemp)167		except:168			print access.listreceivedbyaddress()169	except:170		print "\n---An error occurred---\n"171elif cmd == "listtransactions":172	try:173		acct = raw_input("Account (optional): ")174		count = raw_input("Number of transactions (optional): ")175		frm = raw_input("Skip (optional):")176		try:177			print access.listtransactions(acct, count, frm)178		except:179			print access.listtransactions()180	except:181		print "\n---An error occurred---\n"182elif cmd == "move":183	try:184		frm = raw_input("From: ")185		to = raw_input("To: ")186		amt = raw_input("Amount:")187		mc = raw_input("Minimum confirmations (optional): ")188		comment = raw_input("Comment (optional): ")189		try:190			print access.move(frm, to, amt, mc, comment)191		except:192			print access.move(frm, to, amt)193	except:194		print "\n---An error occurred---\n"195elif cmd == "sendfrom":196	try:197		frm = raw_input("From: ")198		to = raw_input("To: ")199		amt = raw_input("Amount:")200		mc = raw_input("Minimum confirmations (optional): ")201		comment = raw_input("Comment (optional): ")202		commentto = raw_input("Comment-to (optional): ")203		try:204			print access.sendfrom(frm, to, amt, mc, comment, commentto)205		except:206			print access.sendfrom(frm, to, amt)207	except:208		print "\n---An error occurred---\n"209elif cmd == "sendmany":210	try:211		frm = raw_input("From: ")212		to = raw_input("To (in format address1:amount1,address2:amount2,...): ")213		mc = raw_input("Minimum confirmations (optional): ")214		comment = raw_input("Comment (optional): ")215		try:216			print access.sendmany(frm,to,mc,comment)217		except:218			print access.sendmany(frm,to)219	except:220		print "\n---An error occurred---\n"221elif cmd == "sendtoaddress":222	try:223		to = raw_input("To (in format address1:amount1,address2:amount2,...): ")224		amt = raw_input("Amount:")225		comment = raw_input("Comment (optional): ")226		commentto = raw_input("Comment-to (optional): ")227		try:228			print access.sendtoaddress(to,amt,comment,commentto)229		except:230			print access.sendtoaddress(to,amt)231	except:232		print "\n---An error occurred---\n"233elif cmd == "setaccount":234	try:235		addr = raw_input("Address: ")236		acct = raw_input("Account:")237		print access.setaccount(addr,acct)238	except:239		print "\n---An error occurred---\n"240elif cmd == "setgenerate":241	try:242		gen= raw_input("Generate? (true/false): ")243		cpus = raw_input("Max processors/cores (-1 for unlimited, optional):")244		try:245			print access.setgenerate(gen, cpus)246		except:247			print access.setgenerate(gen)248	except:249		print "\n---An error occurred---\n"250elif cmd == "settxfee":251	try:252		amt = raw_input("Amount:")253		print access.settxfee(amt)254	except:255		print "\n---An error occurred---\n"256elif cmd == "stop":257	try:258		print access.stop()259	except:260		print "\n---An error occurred---\n"261elif cmd == "validateaddress":262	try:263		addr = raw_input("Address: ")264		print access.validateaddress(addr)265	except:266		print "\n---An error occurred---\n"267elif cmd == "walletpassphrase":268	try:269		pwd = raw_input("Enter wallet passphrase: ")270		access.walletpassphrase(pwd, 60)271		print "\n---Wallet unlocked---\n"272	except:273		print "\n---An error occurred---\n"274elif cmd == "walletpassphrasechange":275	try:276		pwd = raw_input("Enter old wallet passphrase: ")277		pwd2 = raw_input("Enter new wallet passphrase: ")278		access.walletpassphrasechange(pwd, pwd2)279		print280		print "\n---Passphrase changed---\n"281	except:282		print283		print "\n---An error occurred---\n"284		print285else:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
