Missing socket.AF_BLUETOOTH in Anaconda Python?

做~自己de王妃 提交于 2019-12-10 09:48:00

问题


I'm trying to use socket.AF_BLUETOOTH as explained here: https://docs.python.org/3.3/library/socket.html

I have Python 3.3.5 :: Anaconda 2.1.0 (x86_64) on Mac OS X 10.10.2

and the socket module doesn't seem to contain any AF_BLUETOOTH reference:

In [1]: import socket
In [2]: socket.AF
socket.AF_APPLETALK  socket.AF_INET       socket.AF_IPX        socket.AF_SNA        socket.AF_UNIX       
socket.AF_DECnet     socket.AF_INET6      socket.AF_ROUTE      socket.AF_SYSTEM     socket.AF_UNSPEC     

Can anyone help?


回答1:


The docs say:

Depending on the system and the build options, various socket families are supported by this module.

And from this bit in Modules/socketmodule.c:

#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
#define USE_BLUETOOTH 1

you'll want to make sure that HAVE_BLUETOOTH_H or USE_BLUETOOTH get set true during compilation. Which one depends by the location of your headers file. They can be in /usr/include or /usr/include/bluetooth. You can check your current settings via:

import sysconfig
print sysconfig.get_config_vars()['HAVE_BLUETOOTH_H']

I'm guessing that returns 0 for you currently. A hint from pyconfig.h.in:

/* Define to 1 if you have the <bluetooth/bluetooth.h> header file. */
#undef HAVE_BLUETOOTH_BLUETOOTH_H

so you should make sure that bluetooth/bluetooth.h header file is present on your system and available in your search path during compilation.




回答2:


PF_BLUETOOTH is an protocol family implemented by Linux’s bluetooth module (from BlueZ). On Linux, you create a L2CAP socket and use socket syscalls to communicate with the device (connect, bind, read, write), and the addresses have address family AF_BLUETOOTH. But this is a Linux-only socket type.

On the Mac, you need to use the CoreBluetooth API (which uses XPC messages to the blued daemon) instead to communicate to a Bluetooth LE device.

I’m not aware of a Python wrapper for CoreBluetooth on OS X, but if you want to see how it might look see the node.js libraries bleno or noble. Actually these libraries use the internal XPC messages which might not be too stable instead of the public API.



来源:https://stackoverflow.com/questions/29107537/missing-socket-af-bluetooth-in-anaconda-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!