Outils pour utilisateurs

Outils du site


blog

Exécuter du code python avec go lang

Pb 1

$ go build py.go 
# command-line-arguments
./py.go:3:11: fatal error: Python.h: No such file or directory
    3 | // #include <Python.h>
      |           ^~~~~~

Solution

export C_INCLUDE_PATH=/usr/include/python3.13/

Pb 2

$ LANG=C go build py.go 
# command-line-arguments
/usr/lib/go-1.26/pkg/tool/linux_amd64/link: running x86_64-linux-gnu-gcc failed: exit status 1
/usr/bin/x86_64-linux-gnu-gcc -m64 -Wl,--build-id=0x93e927a0e727ab54037ec0b3c85116f34e35a1b2 -o $WORK/b001/exe/a.out -Wl,--export-dynamic-symbol=_cgo_panic -Wl,--export-dynamic-symbol=_cgo_topofstack -Wl,--export-dynamic-symbol=crosscall2 -Wl,--compress-debug-sections=zlib /tmp/go-link-256395171/go.o /tmp/go-link-256395171/000000.o /tmp/go-link-256395171/000001.o /tmp/go-link-256395171/000002.o /tmp/go-link-256395171/000003.o /tmp/go-link-256395171/000004.o /tmp/go-link-256395171/000005.o /tmp/go-link-256395171/000006.o /tmp/go-link-256395171/000007.o /tmp/go-link-256395171/000008.o /tmp/go-link-256395171/000009.o /tmp/go-link-256395171/000010.o /tmp/go-link-256395171/000011.o /tmp/go-link-256395171/000012.o /tmp/go-link-256395171/000013.o /tmp/go-link-256395171/000014.o /tmp/go-link-256395171/000015.o /tmp/go-link-256395171/000016.o -O2 -g -O2 -g -lpthread -no-pie
/usr/bin/x86_64-linux-gnu-ld.bfd: /tmp/go-link-256395171/000001.o: in function `_cgo_0270074b3d42_Cfunc_PyRun_SimpleString':
/tmp/go-build/cgo-gcc-prolog:55:(.text+0x15): undefined reference to `PyRun_SimpleStringFlags'
/usr/bin/x86_64-linux-gnu-ld.bfd: /tmp/go-link-256395171/000001.o: in function `_cgo_0270074b3d42_Cfunc_Py_Finalize':
/tmp/go-build/cgo-gcc-prolog:67:(.text+0x31): undefined reference to `Py_Finalize'
/usr/bin/x86_64-linux-gnu-ld.bfd: /tmp/go-link-256395171/000001.o: in function `_cgo_0270074b3d42_Cfunc_Py_Initialize':
/tmp/go-build/cgo-gcc-prolog:76:(.text+0x41): undefined reference to `Py_Initialize'
collect2: error: ld returned 1 exit status
2026/04/18 17:00 · Jean-Baptiste

Inclure des appels à Go lang dans Python

Tuto 1

Source : https://dev.to/leapcell/how-to-call-go-code-in-python-accelerate-python-with-go-54if

Linux and Mac

go build -buildmode=c-shared -o add.so add.go

windows

go build -buildmode=c-shared -o add.dll add.go
go build -ldflags "-s -w" -buildmode=c-shared -o add.so add.go

-s indicates compression -w indicates removing debugging

# Test calling code written in the GO language from Python
# First, the GO code needs to be compiled into a dynamic link library
 
from ctypes import cdll
 
lib = cdll.LoadLibrary("./add.so")  # Specify the compiled dynamic link library file here
 
# Call the Add function in the GO language
result = lib.Add(100, 200)
 
print("result ", result)
 
# Call the PrintDll function in the GO language
lib.PrintDll()

Tuto 2

Exemple 1

library.go

package main
 
import (
   "C"
   "log"
)
 
//export helloWorld
func helloWorld(){
   log.Println("Hello World")
}
 
func main(){
 
}

Compilation de la lib

go build -buildmode=c-shared -o library.so library.go

Execution depuis Python

import ctypes
library = ctypes.cdll.LoadLibrary('./library.so')
hello_world = library.helloWorld
hello_world()
Exemple 2

library.go

//export hello
func hello(namePtr *C.char){
   name := C.GoString(namePtr)
   log.Println("Hello", name)
}

app.py

hello = library.hello
hello.argtypes = [ctypes.c_char_p]
hello("everyone".encode('utf-8'))

library.go

//export farewell
func farewell() *C.char{
   return C.CString("Bye!")
}

FIXME

2026/04/18 16:14 · Jean-Baptiste

Notes protocoles TCP/IP - IGMP et multicast

Voir multicast :

Voir IGMP :

iptables règles multicast :

Multicast IGMP (Internet Group Management Protocol) :

  • 224.0.0.0/4

Filtre Wireshark pour IGMP et mulicast :

igmp or ip.addr==224.0.0.0/4

Règle iptables pour autoriser IGMP

iptables -I INPUT -p igmp -j ACCEPT
iptables -I FORWARD -p igmp -j ACCEPT

Règle pour bloquer le multicast

iptables -A INPUT -m pkttype --pkt-type multicast -j DROP
iptables -A INPUT   -m pkttype --pkt-type multicast -j ACCEPT
iptables -A FORWARD -m pkttype --pkt-type multicast -j ACCEPT
iptables -A OUTPUT  -m pkttype --pkt-type multicast -j ACCEPT
 
# Or:
 
iptables -A INPUT   -s 224.0.0.0/4 -j ACCEPT
iptables -A FORWARD -s 224.0.0.0/4 -d 224.0.0.0/4 -j ACCEPT
iptables -A OUTPUT  -d 224.0.0.0/4 -j ACCEPT

Source : https://gist.github.com/juliojsb/00e3bb086fd4e0472dbe#file-iptables-multicast-sh

FIXME

2026/04/15 09:42 · Jean-Baptiste

Notes go lang programmation fonctionnelle

map

Voir :

package main
 
import "fmt"
 
//type f_ii func(int) int
 
//func MapInt(f f_ii, l []int) [](int) {
func MapInt(f func(int) int, l []int) [](int) {
        ret := []int{}
        for _, v := range l {
                ret = append(ret, f(v))
        }
        return (ret)
}
 
func carre(i int) int {
        return i * i
}
 
func main() {
        liste := []int{1, 2, 3}
        fmt.Println(MapInt(carre, liste))
}

Voir aussi

package main
 
import "fmt"
                                            type FuncType func(int) int
 
func square(x int) int {
    return x * x                            }
 
var f FuncType = FuncType(square)
 
func main() {
 fmt.Println(f(5))
}

Autres

func stringInSlice(a string, list []string) bool {
        for _, b := range list {
                if b == a {
                        return true
                }
        }
        return false
}

FIXME

2026/04/13 22:27 · Jean-Baptiste
,

Notes ping ICMP

rootless

Allowing ping

Most distributions do not allow non-root users to send ICMP Echo Request packets (aka ping) by default.

To allow running ping without root, create /etc/sysctl.d/99-rootless.conf with the following content:

/etc/sysctl.d/99-rootless.conf

net.ipv4.ping_group_range = 0 2147483647

Then run the following command to reload the new sysctl configuration:

sudo sysctl --system

Source : https://rootlesscontaine.rs/getting-started/common/sysctl/

FIXME

2026/04/03 23:01 · Jean-Baptiste
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki