2024-05-27
Ip Info Collect

Tutorial:

https://stackoverflow.com/questions/24678308/how-to-find-location-with-ip-address-in-python

To obtain IP of ourselves, we can visit:

1
2
curl https://api.ipify.org

To get geo info of our IP, visit:

1
2
curl https://ipinfo.io | jq .country

TO get geo info of any IP, use:

https://pypi.org/project/IP2Location/

https://ip2location-python.readthedocs.io/en/latest/quickstart.html

Read More

2024-04-23
Python Object Value Shorthand

Use sorcery

1
2
3
4
import sorcery
a,b,c = 1,2,3
mydict = sorcery.dict_of(a,b,c)

Related question

Read More

2024-02-24
Using Tensorboard

1
2
3
pip3 uninstall tb-nightly tensorboardX tensorboard
python3 -m tensorboard.main --help

Read More

2024-01-06
Unbuffered Python

When reading bytes asynchronously from stdout using asyncio.create_subprocess_exec, the program has to be unbuffered.

1
2
python3 -u <script_path>

1
2
3
4
5
6
7
8
9
10
11
# this can only make `print` into unbuffered
import builtins
import copy
old_print = copy.copy(builtins.print)
def custom_print(*args, **kwargs):
if 'flush' not in kwargs:
kwargs['flush'] = True
old_print(*args, **kwargs)
# Override the built-in print function with the custom function
builtins.print = custom_print

Read More

2023-12-27
Defaultdict And Counter

from collections import defaultdict, Counter
mydict = defaultdict(Counter) # remember to put callable or none as argument of defaultdict
mydict = defaultdict(lambda: defaultdict(lambda: 0)) # alternative
mydict['k1']['k2'] += 1 # two is ok, but not one or three
Read More

2023-07-05
Python Encoding Issue

windows has encoding issue on python intepreter.

run like this:

1
2
3
python -X utf8=1 <args>
# flag: sys.flags.utf8_mode

Read More

2023-02-19
Generate Docx Document From Python Docstring

install and use pdoc3

1
2
3
pip install pdoc3
pdoc --html [-o <output_dir>] <python_script_or_module_path> # default output directory of "html" is `./html`

install and use pandoc, on its homepage we find some slideshow backends like reveal.js, dzslides, s5, slideous and slidy (alternative to microsoft powerpoint, may help rendering video, or let’s use libreoffice instead? or some dedicated video editing library like moviepy)

1
2
3
# let's convert the html version of
pandoc -o <output_docx_filename> <input_html_path>

remove unwanted parts from html (beautifulsoup), and split index from main content (split and concat with docxcompose)

for composing docx from hand, use python-docx. for template based docx generating, use docxtpl

to insert page break into converted docx, there are two ways (maybe):

  1. change css in the original html code

  2. insert page break while concatenating

Read More

2023-02-17
Example Pydoc


description: |

API documentation for modules: example_docstring.

lang: en

classoption: oneside

geometry: margin=1in

papersize: a4

linkcolor: blue

links-as-notes: true

Module example_docstring

Read More

2022-12-10
Pwntools Usage Example

I created a script for solving a simple problem on RCTF.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from pwn import *
ip = "190.92.234.114"
port = 23334
mclean =lambda l0: l0.decode().split("=")[1].strip()
mlist = lambda T: [int(x) for x in T.replace("[","").replace("]", "").replace(" ","").strip().split(",")]
r = remote(ip, port)
l0 = r.recvline()
# print('first line?', l0) # great man!
q = mclean(l0)
q = int(q)
# but notice you will not like to be fucked up. use safe eval? ast?
l1 = r.recvline()
# print('second line?', l1)
T = mclean(l1)
T = mlist(T)
l2 = r.recvline()
U = mlist(mclean(l2))
# print("third line?", l2)
print("Q?", q)
print()
print("T?", len(T))
print()
print("U?", len(U))
# now crack the x. please observe the original code?
# the shift does not matter so much?
mpos_x = {}
for i in range(90):
t = T[i]
u = U[i]
pos_x = u//t+1
mpos_x.update({pos_x:mpos_x.get(pos_x,0)+1})
mfinalPos = [(key, elem) for key, elem in mpos_x.items()]
mfinalPos.sort(key=lambda x: -x[1])
print("NUM?",mfinalPos[0])
print("COUNT?",mfinalPos[0][1])
# import pyperclip
data =str(mfinalPos[0][0])
# pyperclip.copy(data)
# r.interactive()
r.sendline(data.encode())
flag=r.recvline() #EOFERROR?
print("FLAG?",flag)
# now answer the shit?

Read More

2022-11-11
Call Ruby From Python

Read More