> For the complete documentation index, see [llms.txt](https://staphysec.gitbook.io/staphysec/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://staphysec.gitbook.io/staphysec/programming-and-scripting/python.md).

# Python

## Libraries

* Requests ([Documentaton](https://docs.python-requests.org/en/latest/)).
* OS PATH ([Doc](https://www.geeksforgeeks.org/os-path-module-python/))

## Cheatsheet

```
# Run commands throught request exec
data = {'ingredient': 'our_var', 'measurements': '__import__("os").popen("ls").read()'}
```

## Subprocess&#x20;

```
print(check_output(cmd).decode().strip(,end='\r')
server = datetime.strptime(requests.head(URL).headers['Date'], '%a, %d %b %Y %X %Z')
offset = int((server - kali).total_seconds())
f'--use-time={"%+d" % offset}' # %+d 'd for decimal / + for

```

## Time

```
datetime.utcnow() # get current time
```

```
os.system('java -jar ysoserial.jar %s "%s" > %s.txt' % (payload, cmd, payload))
payload_file = open('./' + payload + '.txt', 'rb')
```

## BeautifulSoup&#x20;

Link [tutorial](https://www.dataquest.io/blog/web-scraping-python-using-beautiful-soup/). Web Scraping with Python Using Beautiful Soup

## Functions

### Startwith()

The `startswith()` method returns True if the string starts with the specified value, otherwise False.

*string*.startswith`(_value, start, end_)`

*value* : Required. The value to check if the string starts with

*start* : Optional. An Integer specifying at which position to start the search

*end* : Optional. An Integer specifying at which position to end the search

### Split()

the `split()` method splits a string into a list. You can specify the separator, default separator is any whitespace.

> **Note:** When maxsplit is specified, the list will contain the specified number of elements *plus one*.

*string*.split(*separator, maxsplit*)\[0,1,2] etc to specify which item to show

*separator* : Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator

*maxsplit* : Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences".

### Join()

The `join()` method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

```python
myTuple = ("John", "Peter", "Vicky")  
  
x = "#".join(myTuple)  
  
print(x)
## John#Peter etc
```

### Rstrip()

The rstrip() method **removes trailing spaces or characters from the right side of a string**. The program will remove all white space characters by default if you do not specify a character to remove.

```
variable.rstrip()
```
