4

I am trying to read the value from an address in Python.

Suppose I have an address in variable: address_vble= 0x900045A1

In C, we can just get value = *address_vble

How can I do the same in Python?

Thanks for assistance.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
Prashu Pratik
  • 41
  • 1
  • 2
  • 6
  • 4
    You can't. Python doesn't let one directly access memory. – ForceBru Sep 28 '18 at 11:02
  • Possible duplicate of [Is it possible to dereference variable id's?](https://stackoverflow.com/questions/15011674/is-it-possible-to-dereference-variable-ids) – strippenzieher Sep 28 '18 at 11:03
  • 5
    How did you get that address in the first place? Does that address space belong to the Python process? How'd you get the address then? Or does it belong to another process? You probably can't access it then. – deceze Sep 28 '18 at 11:03
  • @deceze The address is a valid point in my hardware. I can see what address is there on that address. I just want to read the value and store it in a variable – Prashu Pratik Sep 28 '18 at 11:16
  • 2
    Possible duplicate of [Access memory address in python](https://stackoverflow.com/questions/8250625/access-memory-address-in-python) – James Sep 28 '18 at 11:18
  • 1
    See the answer at https://stackoverflow.com/questions/8250625/access-memory-address-in-python/8250902#8250902. You need to know what the length of the value stored at the memory address you are accessing. – James Sep 28 '18 at 11:19

3 Answers3

15

You can find it by ctypes

>>>import ctypes
>>>a = 5
>>>address = id(a)
>>>address
493382800
>>>ctypes.cast(address, ctypes.py_object).value
5

Hope it will help you!

JON
  • 1,668
  • 2
  • 15
  • 18
  • I guess what you have written is an example of how to get the address of the variable 'a'. But in my case, I already know the memory address. That is address_vble= 0x900045A1. So what I want is to get the value stored on that address. How can I do that? – Prashu Pratik Sep 28 '18 at 11:18
  • id(a) will give the address of variable a, you can any other address there, it will work, To show you i have given like this – JON Sep 28 '18 at 11:26
  • @PrashuPratik, I have changed the code bit to make you understand, Hope, it will give you more understanding !!! – JON Sep 28 '18 at 11:32
  • will it only take decimal format of 'address'? I tried this code with the hex address value (mentioned above), it is crashing. – Prashu Pratik Sep 28 '18 at 11:43
  • I tried converting the address to decimal format. But still it is crashing on this step : **ctypes.cast(address, ctypes.py_object).value** – Prashu Pratik Sep 28 '18 at 11:47
  • Thank you so much! That's exactly what I was looking for! Experimenting with mutable and immutable objects. This code can help proving that a is immutable, if you change ```a``` to another value, say, 4 and then call ```ctypes.cast(address, ctypes.py_object).value``` again. Good article here https://freecontent.manning.com/mutable-and-immutable-objects/. Hope it helps someone else as well. – ShHolmes Feb 12 '21 at 09:27
1

@PrashuPratik we can convert hexa-decimal address into integer and then we can check the value at any memory address.

import ctypes
    x=id(a)
    x
    493382800
    y=hex(x)
    y
   '0xc545d0'
    z=int(y,base=16)  #this will convert the hexadecimal value into integer value
    ctypes.cast(x,ctypes.py_object).value
    5

int('hexadecimal',base=16) by using this you can convert any hexadecimal number into integer.

I hope this will resolve your problem

Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28
Anuj Dagar
  • 11
  • 1
0

Accessing value through memory address

Example-1

import ctypes
sn = 1 # Value to store on sn
value1 = sn # Value of sn
print("value1 =",value1)
memory_address = id(sn) # Get address of sn variable
value2 = ctypes.cast(memory_address, ctypes.py_object).value #Get value from  address of sn variable
print("value2 =",value2)

Example-2

import ctypes
value3 = 100
memory_address2=id(value3) # Getting address of variable
print("value 3 =", value3)
print("Memory Address (INT) =",memory_address2)
memory_address3=hex(memory_address2) # Integer to hex conversion
print("Memory Address (HEX) =",memory_address3)
memory_address4=int(memory_address3, base=16) # Revert to Integer Type from hex
print("Memory Address (INT) =",memory_address4)
value4 = ctypes.cast(memory_address4, ctypes.py_object).value # Getting value of address
print("value 4 =", value4)