sls hat geschrieben:
Wie öffne ich in Python ein TCP6 Socket, das wie folgt auf Localhost abläuft, richtig?
tcp6 0 0 [::]:3030 [::]:* LISTEN
Ich habe es gerade mal ausprobiert, das scheint so zu funktionieren:
Code: Alles auswählen
import socket
HOST = '::'
PORT = 3030
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.bind((HOST, PORT))
:: ist die Local Unicast Adresse, das entspricht den alten privaten IPv4-Bereichen in lokalen, nicht im Internet gerouteten Netzen.
netstat -tulpn spuckt dann das hier aus:
[codebox=bash file=Unbenannt.bsh]tcp6 0 0 :::3030 :::* LISTEN 4242/python[/code][/quote]
Leider will das nicht klappen:
Code: Alles auswählen
import sys
import usb.core
from time import sleep
import socket #https://wiki.python.org/moin/TcpCommunication
HOST = '::'
PORT = 3030
BUFFER_SIZE = 15
dev = usb.core.find(idVendor=0x16c0, idProduct=0x5dc)
assert dev is not None
print hex(dev.idVendor) + ', ' + hex(dev.idProduct)
while True:
ret = dev.ctrl_transfer(0xC0, 4, 0, 0, 200) # array('B', [107, 24])
cB = (ret[0] + ((ret[1] & 3) * 256)) + 300
dB = cB * 0.1
AK = cB / 5 + 50
print dB
print cB
print AK
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.bind((HOST, PORT))
#data = s.recv(BUFFER_SIZE)
#if data is None:
# s.send(AK)
#else:
# s.send(data)
sleep(0.99) # Time in seconds.
Läuft somit ohne Fehlermeldung mit Python2, nur netstat zeigt kein Socket 3030 an:
Code: Alles auswählen
~ $ netstat -a -p | grep tcp
(Es konnten nicht alle Prozesse identifiziert werden; Informationen über
nicht-eigene Processe werden nicht angezeigt; Root kann sie anzeigen.)
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:1880 0.0.0.0:* LISTEN 4604/node-red
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN -
tcp 0 0 localhost:46958 localhost:43605 VERBUNDEN -
tcp 0 0 localhost:43605 localhost:46958 VERBUNDEN 709/vncserverui
tcp6 0 0 [::]:ssh [::]:* LISTEN -
tcp6 0 0 [::]:5900 [::]:* LISTEN
Mit Python3 meckert es, um die Library USB.core...