Python3.6 上的模块套接字出错

Error with module Socket on Python3.6

首先是上下文:我想通过蓝牙连接发送一些数据。 为此,我想使用 Python 3.6,因为我使用这种编程语言编写了算法的其余部分。我在基于 Debian Jessie 的 Raspbian Jessie lite for raspberry pi 上使用它。 问题是当我使用这段代码时:

import socket

hostMACAddress = 'xx:xx:xx:xx:xx:xx' # The MAC     address of a Bluetooth adapter on the server
port = 3 
backlog = 1
size = 1024
s=socket.socket(socket.AF_BLUETOOTH,socket.SOCK_STREAM,socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
try:
    client, address = s.accept()
    while 1:
        data = client.recv(size)
        if data:
            print("data")
            client.send(data)
except: 
    print("Closing socket") 
    client.close()
    s.close()

这个错误看起来像:

Traceback (most recent call last):
  File "TestSocket.py", line 5, in <module>
    s =socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'

我从一个站点使用它,表明它适用于 Python 3.3 及更高版本。 这很复杂,因为没有这个属性我不知道如何使用套接字模块建立蓝牙连接并正确发送数据。 我不是 Python 方面的专家,因此欢迎任何帮助或使用 Debian 发行版。 Thinx for you time and sorry for any syntax error or spellings 英语不是我的母语。

Raspbian 安装了两个 Python 版本,2.7 和 3.4。

Python 2.7 是默认值,因此在命令提示符下键入 python script.py 将 运行 针对 Python 2.7

的脚本

如果您使用 python3 script.py,您应该会发现您的脚本有效

另请注意,可用于 Raspbian Jessie 的最新 Python 版本是 3.4