ENet Python (zpl-c/enet Python Bağlayıcıları)
zpl-c/enet C kütüphanesi için modern, yüksek performanslı ve Pythonik bağlayıcı (binding) kütüphanesi.
ENet, UDP protokolü üzerinde güvenilir (reliable), sıralı (in-order) veya sırasız (unsequenced) paket iletimi, parçalama (fragmentation) ve çoklu kanal (channel) mimarisi sunan hafif bir ağ kütüphanesidir. Özellikle çok oyunculu (multiplayer) oyunlar ve gerçek zamanlı simülasyonlar için standarttır.
Bu bağlayıcı, zpl-c/enet çatalının sunduğu yenilikleri (tam entegre IPv6 desteği, monotonik zamanlayıcı, güvenli özellik erişimleri) Python dünyasına taşır ve klasik pyenet kütüphanesiyle API uyumluluğu sağlar.
Özellikler
- Entegre IPv6 & IPv4 Çift Yığın (Dual-Stack):
zpl-c/enetaltyapısı sayesinde hem IPv4 hem de IPv6 adresleri sorunsuz çalışır. - Güvenilir & Güvensiz Paket Gönderimi:
PACKET_FLAG_RELIABLE,PACKET_FLAG_UNSEQUENCEDve parçalanmış paket desteği. - Çoklu Kanal Mimarisi: Farklı veri akışları için bağımsız mantıksal kanallar (0-255).
- Pythonik Nesne Yönelimli API:
Host,Peer,Packet,Address,Eventsınıfları ve context manager (with enet.Host(...)) desteği. - Otomatik Bellek Yönetimi: Paket ve oturum belleklerinin güvenli biçimde temizlenmesi (
__del__vedestroy()). - Sıfır Harici Derleme Bağımlılığı: Windows x64 için hazır derlenmiş native kütüphane (
enet.dll) paket içerisinde gelir; Linux ve macOS için tek tıkla derlenebilir (build_native.py).
Kurulum
Projeyi doğrudan çalışma dizininize dahil edebilir (import enet) veya pip ile kurabilirsiniz:
# PyPI üzerinden kurulum
pip install enet2
# Geliştirici / düzenlenebilir modda kurulum
pip install -e .
Hızlı Başlangıç (Quickstart)
1. Sunucu (Server) Örneği
import enet
# Wildcard (0.0.0.0 / ::) adresinde 12345 portunu dinle
server_address = enet.Address(None, 12345)
with enet.Host(server_address, peer_count=32, channel_limit=2) as server:
print(f"Sunucu başlatıldı: {server_address}")
running = True
while running:
event = server.service(timeout_ms=50)
if event.type == enet.EVENT_TYPE_CONNECT:
print(f"[+] İstemci bağlandı: {event.peer.address} (ID: {event.peer.id})")
event.peer.data = {"username": "Misafir"}
elif event.type == enet.EVENT_TYPE_RECEIVE:
mesaj = event.packet.data.decode("utf-8")
print(f"<- Gelen paket [{event.channel_id}]: {mesaj}")
# Yanıt paketi gönder
yanit = f"Echo: {mesaj}".encode("utf-8")
event.peer.send(event.channel_id, enet.Packet(yanit, enet.PACKET_FLAG_RELIABLE))
server.flush()
elif event.type in (enet.EVENT_TYPE_DISCONNECT, enet.EVENT_TYPE_DISCONNECT_TIMEOUT):
print(f"[-] İstemci ayrıldı: {event.peer.address}")
2. İstemci (Client) Örneği
import time
import enet
# İstemci host'u oluştur (yerel porta bağlanmaz, giden bağlantı açar)
with enet.Host(None, peer_count=1, channel_limit=2) as client:
hedef = enet.Address("127.0.0.1", 12345)
peer = client.connect(hedef, channel_count=2, data=100)
# Bağlantı kurulmasını bekle
connected = False
start = time.time()
while time.time() - start < 5.0:
event = client.service(timeout_ms=50)
if event.type == enet.EVENT_TYPE_CONNECT:
connected = True
print(f"[+] Sunucuya bağlanıldı! RTT: {peer.round_trip_time}ms")
break
if connected:
# Güvenilir (reliable) paket gönder
paket = enet.Packet(b"Merhaba ENet!", enet.PACKET_FLAG_RELIABLE)
peer.send(channel_id=0, packet=paket)
client.flush()
# Yanıt bekle
start = time.time()
while time.time() - start < 2.0:
event = client.service(timeout_ms=50)
if event.type == enet.EVENT_TYPE_RECEIVE:
print(f"<- Sunucudan gelen: {event.packet.data.decode('utf-8')}")
break
# Nazikçe bağlantıyı kes
peer.disconnect(data=0)
client.flush()
API Referansı
enet.Address(host: str | None = None, port: int = 0)
Bir ağ adresini (IP / alan adı ve port) temsil eder.
host: IP adresi ("127.0.0.1", "::1"), alan adı veyaNone(tüm arayüzler).port: Port numarası (0-65535).ip: Düzeltilmiş IP adresi dizesi.is_ipv6: Native IPv6 adresi olup olmadığı (bool).is_any: Wildcard / dinleme adresi olup olmadığı (bool).
enet.Host(address, peer_count=32, channel_limit=2, incoming_bandwidth=0, outgoing_bandwidth=0)
Bağlantıları yöneten ana ENet host sınıfı.
service(timeout_ms=0) -> Event: Ağ olaylarını bekler ve döndürür.poll(timeout_ms=0) -> Iterator[Event]: Tüm bekleyen olayları yield eden üreteç.connect(address: Address, channel_count=2, data=0) -> Peer: Yeni bir bağlantı başlatır.broadcast(channel_id: int, packet: Packet): Bağlı tüm peer'lara paketi dağıtır.flush(): Kuyruktaki paketleri hemen gönderir.destroy(): Host'u kapatır ve soketi serbest bırakır.- Özellikler:
peers_count,packets_sent,packets_received,bytes_sent,bytes_received,mtu.
enet.Peer
Bağlantı kurulan uzak düğümü (istemci veya sunucu) temsil eder.
send(channel_id: int, packet: Packet) -> int: Belirtilen kanala paket kuyruklar.receive() -> tuple[int, Packet] | None: Alınan paketi ve kanalını çeker.ping(): RTT ölçümü ve bağlantı canlılığı için ping gönderir.disconnect(data=0): Nazik bağlantı kesme isteği gönderir.disconnect_now(data=0): Hemen bağlantıyı sonlandırır.disconnect_later(data=0): Bekleyen paketler iletildikten sonra bağlantıyı sonlandırır.data: Kullanıcının bu peer ile ilişkilendirebileceği özel Python verisi (örn.peer.data = {"player_id": 5}).- Özellikler:
id,address,state,round_trip_time(RTT),packets_sent,packets_lost,packet_loss,bytes_sent,bytes_received.
enet.Packet(data: bytes | str, flags: int = PACKET_FLAG_RELIABLE)
Ağ üzerinden iletilen veri paketi.
data: Bayt (bytes) cinsinden veri.data_length: Veri uzunluğu.flags: Paket bayrakları (PACKET_FLAG_RELIABLE,PACKET_FLAG_UNSEQUENCED, vb.).reliable: Paketin güvenilir olup olmadığı.destroy(): Paketi bellekten temizler.
enet.Event
host.service() tarafından döndürülen ağ olayı.
type: Olay tipi (EVENT_TYPE_CONNECT,EVENT_TYPE_RECEIVE,EVENT_TYPE_DISCONNECT,EVENT_TYPE_NONE).peer: Olayın ait olduğuPeernesnesi.channel_id(veyachannelID): Olayın gerçekleştiği kanal numarası.data: Bağlantı veya ayrılma ile ilişkili kullanıcı verisi.packet:EVENT_TYPE_RECEIVEdurumunda alınanPacketnesnesi.
Testleri Çalıştırma
Kütüphanenin tüm birim testlerini pytest ile çalıştırabilirsiniz:
pytest tests/ -v
Lisans
Bu proje MIT lisansı altında lisanslanmıştır. Orijinal ENet telif hakkı Lee Salzman, zpl-c/enet telif hakkı Vladyslav Hrytsenko ve Dominik Madarász'a aittir.
Metadata
Release files for enet2 2.7.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| enet2-2.7.2.tar.gz | 155.0 kB | Without provenance | Details for enet2-2.7.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| enet2-2.7.2-py3-none-any.whl | Python 3 | none | any | Details for enet2-2.7.2-py3-none-any.whl |
Total release size: 305.4 kB
Release files / enet2-2.7.2.tar.gz
| Download URL | enet2-2.7.2.tar.gz |
|---|---|
| Size | 155.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
044e7b0f4d71671ae2e363b76252b85666f8e11e8fc7ceeaa0351a474693a324
|
|
BLAKE2b-256 checksum How to use checksums |
1d88ef71806140e5ef07528e38ae095822a9d095d98695a8679e3cafa01f2844
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.17
|
Release files / enet2-2.7.2-py3-none-any.whl
| Download URL | enet2-2.7.2-py3-none-any.whl |
|---|---|
| Size | 150.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3ecf39e719d57a95e23cc630fef833dfd16e0d0cc2e184e766da205ff9c341f1
|
|
BLAKE2b-256 checksum How to use checksums |
7adba2e293242e1897fc43c4be8f69f8a6379c87eae0fa509d42626aed339437
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.17
|
Security for enet2 2.7.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.