PY
text/x-python
Magic Bytes
Offset: 0
23 21 2F 62 69 6E 2F 70 79 74 68 6F 6E
Python Source Code (PY) is a high-level, interpreted programming language file format developed by Guido van Rossum and maintained by the Python Software Foundation. It is primarily utilized for web development, data analysis, artificial intelligence, and automation scripting across diverse operating systems. Although the format consists of plain text, executing these files can run arbitrary code on a host system, necessitating careful review when handling scripts from unverified origins.
Validation Code
How to validate .py files in Python
Python
def is_py(file_path: str) -> bool:
"""Check if file is a valid PY by magic bytes."""
signature = bytes([0x23, 0x21, 0x2F, 0x62, 0x69, 0x6E, 0x2F, 0x70, 0x79, 0x74, 0x68, 0x6F, 0x6E])
with open(file_path, "rb") as f:
return f.read(13) == signature
How to validate .py files in Node.js
Node.js
function isPY(buffer: Buffer): boolean {
const signature = Buffer.from([0x23, 0x21, 0x2F, 0x62, 0x69, 0x6E, 0x2F, 0x70, 0x79, 0x74, 0x68, 0x6F, 0x6E]);
return buffer.subarray(0, 13).equals(signature);
}
Go
func IsPY(data []byte) bool {
signature := []byte{0x23, 0x21, 0x2F, 0x62, 0x69, 0x6E, 0x2F, 0x70, 0x79, 0x74, 0x68, 0x6F, 0x6E}
if len(data) < 13 {
return false
}
return bytes.Equal(data[:13], signature)
}
API Endpoint
GET
/api/v1/py
curl https://filesignature.org/api/v1/py