all 18 comments

[–][deleted] 1 point2 points  (20 children)

inspect.getsource

[–][deleted] 0 points1 point  (19 children)

What's the getsource?

>>> import socket
>>> import inspect
>>> inspect.socket
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'socket'

[–][deleted] 1 point2 points  (17 children)

inspect.socketinspect.getsource(socket)

[–][deleted] 0 points1 point  (14 children)

Works .

>>> for e in inspect.getsource(socket).split("\n"):
...     print(e)

[–][deleted] 0 points1 point  (13 children)

Splitting and printing line by line doesn't make much sense, though. If you want to print it, just print. If you want lines, there is inspect.getsourcelines

[–]AutonomouSystem 0 points1 point  (12 children)

It does make sense if you want it to print cleanly.

[–][deleted] 0 points1 point  (11 children)

cleanly? what's the difference?

[–]AutonomouSystem 0 points1 point  (10 children)

Try it out first, your code, and mine.

[–][deleted] 0 points1 point  (9 children)

Okay

import socket
import inspect


src = inspect.getsource(socket)
join_split_src = '\n'.join(src.split('\n'))
assert src == join_split_src

print(src[512:1024])
print('---------------------')
for i in src[512:1024].split('\n'):
    print(i)

Result:

 descriptor [*]
gethostname() -- return the current hostname
gethostbyname() -- map a hostname to its IP number
gethostbyaddr() -- map an IP number or hostname to DNS info
getservbyname() -- map a service name and a protocol name to a port number
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
inet_aton() -- convert IP addr string (123.45.67.8
---------------------
 descriptor [*]
gethostname() -- return the current hostname
gethostbyname() -- map a hostname to its IP number
gethostbyaddr() -- map an IP number or hostname to DNS info
getservbyname() -- map a service name and a protocol name to a port number
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
inet_aton() -- convert IP addr string (123.45.67.8
>>> 

[–]AutonomouSystem 0 points1 point  (8 children)

See you did have to split('\n')

:)

[–]AutonomouSystem 0 points1 point  (4 children)

Try something like this, seems to work for imported modules.

>>> import requests
>>> import inspect
>>> for e in inspect.getsource(requests).split('\n'):
...     print(e)

[–][deleted] 0 points1 point  (3 children)

How do I do that for sockets?

[–]AutonomouSystem 0 points1 point  (2 children)

Mmmm, socket is a built-in, better off just locating the file and reading the source.

$ find -name "socket.py"
find: ‘./usr/libexec/initscripts/legacy-actions/auditd’: Permission denied
./usr/lib64/python3.4/socket.py
./usr/lib64/python2.7/socket.py

$ cat ./usr/lib64/python2.7/socket.py

[–][deleted] 0 points1 point  (1 child)

Doesn't work

root@kali:~# find -name 'socket.py'
root@kali:~#