> For the complete documentation index, see [llms.txt](https://dhaneshsivasamy07.gitbook.io/oscp-2022/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dhaneshsivasamy07.gitbook.io/oscp-2022/misc/programming-notes-for-offensive-security/python.md).

# Python

Python modules and usage tips that I use often

## Misc

* Get a dynamically updated print output

```bash
for i in range(182930):
    print(f"\rNow the value is{i}", flush=False, end="")
```

* Running a python script with `-i` flag brings us to the end of the exectuion and prompts a python shell

```bash
pyhton3 -i exploit.py
```

* To inspect what are the supported methods available from the output object the `dir` can be used

```python
resp = requests.get("https://dhaneshsivasamy07.gitbook.io")

# look at the supported attributes
dir(resp)
# access the method
resp.text 
resp.elapsed
resp.status_code
# can be further inspected
dir(resp.text)
dir(resp.elapsed)
# accessing
resp.elapsed.microseconds
resp.elapsed.total_seconds()
```

## Requests

* Requests module are used to make requests to the webpage

```python
import requests
url = "https://google.com"

# make a get request
r = requests.get(url=url)
# know the status code
print(r.status_code)
# print the contents of the respone
print(r.text)

# make a post request
p = requests.post(url=url)
# status code and contents can be accessed with p.status_code and p.text

# send a data along with the post request
data = {"user" : "dnoscp", "password" : "iamfastasfuckboii"}
url = "http://127.0.0.1/login.php"
dn = requests.post(url=url, data=data)

# make the requst go through a proxy
data = {"user" : "dnoscp", "password" : "iamfastasfuckboii"}
url = "http://127.0.0.1/login.php"
proxy = {"http" : "http://127.0.0.1:8080"}
dnp = requests.post(url=url, data=data, proxies=proxy)

# When handling with forms and ajax requests, make use of the Session() which hold
# the information that is processed and will use in the subsequent request
# Session() is a function in requests module
session = Session()
get_ = session.get(url=url)
post_ = session.post(url=url, data=data, proxies=proxy)
```
