# 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)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dhaneshsivasamy07.gitbook.io/oscp-2022/misc/programming-notes-for-offensive-security/python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
