Server code:
import bluetooth
server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
server_sock.bind(("",port))
server_sock.listen(1)
client_sock,address = server_sock.accept()
print ("Accepted connection from ",address)
data = client_sock.recv(1024)
print ("received [%s]" % data)
client_sock.close()
server_sock.close()
client code:
import bluetooth
bd_addr = " 50:29:f5:36:ed:00 " #bluetooth address of mobile
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr ,1))
sock.send("hello!!")
sock.close()
Iam getting OS error A which is as shown below
Traceback (most recent call last):
File "C:\Users\katrer2\Bluetooth_communication\Bluetooth\example2.py", line 9, in
sock.connect((bd_addr ,1))
File "C:\Users\katrer2\AppData\Local\Programs\Python\Python37\lib\site-packages\bluetooth\msbt.py"
line 96, in connect
bt.connect (self._sockfd,addr,port)
OSError: A
Python version 3.7.5.
Please help me to find out where I am going wrong or suggest any other solution via which I can connect to a device using Bluetooth.
It looks like it might be an issue with the pybluez library on Windows 10:
https://github.com/pybluez/pybluez/issues/349
Related
I wanted to watch films and other pieces of stuff in turkey, so I had to use my VPN (windscripe) on my Mac. Right now I am in Germany back, and I deinstalled my VPN, so I am not using it. The problem is that when I want to connect to my socket in python I get the error code OSError [Errno 49] Can't assign the requested address. I wanted to look if it is a general problem regarding my Internet Connection, but on my other PC, the IP Address works fine. The problem is only on my mac.
Code:
import socket
import sys
sep = "#"
server_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
server_socket.bind(('2a02:908:1652:5e0:422:a319:ff7b:43e3',1337))
server_socket.listen(2)
liste = []
print("Is starting")
while True:
print("waits on connection")
(client_socket, addr) = server_socket.accept()
msg = client_socket.recv(1024)
liste.append(msg)
Terminal:
Traceback (most recent call last):
File "/Users/admin/Desktop/server/Server.py", line 5, in <module> server_socket.bind(('2a02:908:1652:5e0:422:a319:ff7b:43e3',1337))
OSError: [Errno 49] Can't assign requested address
I restarted my MAC and it didn't work. I restarted my WLAN-Router and it didn't work. It is only my mac.
I'm trying to connect myself with 2 pythons little programs while using socket.
1st program:
#server.py
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1' #L'IP du Serveur
port = 1234 #data transfering port
server.bind((host,port)) #bind server
server.listen(5)
client, addr = server.accept()
print("Got Connection from",addr)
client.send("Hello World :)".encode('UTF-8')) #send data to client
msg = client.recv(1024)
print(msg.decode('UTF-8'))
input()
2nd program:
#client.py
import socket
server = socket.socket()
host = '127.0.0.1' #L'IP du Serveur
port = 1234
server.connect((host,port))
msg =server.recv(1024)
print(msg.decode('UTF-8'))
server.send('Client Online ...'.encode('UTF-8'))
input()
I first run server.py, no problems. Than, I run client.py but when I run it I have:
"
Traceback (most recent call last):
File "/Users/user/Documents/client.py", line 8, in <module>
server.connect((host,port))
ConnectionRefusedError: [Errno 61] Connection refused
>>>
"
I tried multiple things like desactivate my wall fire, put my 192.168.1.x IP but still have the same message error. I also send that to one of my friends that is on a PC (I'm on a MAC) and he has no problems. So I guess that the problem is because of the fact that I have a mac. Someone have an answer or an explanation ?
I was coding with IDLE. It was the problem. I guess that IDLE has a protection that doesn't allow people to do sockets. So I just went to Terminal and it finally works.
I am trying to get the data from a blood pressure monitor using the python in raspberry pi3. I googled and found a few examples to get the data using python.
My code:
#!/usr/bin/python
import serial
neo = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0
)
print("connected to: " + neo.portstr)
#neo.open() #opens port
print "Is port open ? ",neo.isOpen() #returns true?
#ser.write("help\n");
while True:
dataline = neo.readline();
if dataline:
print(dataline), neo.close()
When I ran above code using "sudo python pyusb.py" command, it is returning the following error:
connected to: /dev/ttyUSB0
Is port open ? True
None
Traceback (most recent call last):
File "pyusb.py", line 18, in <module>
dataline = neo.readline();
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 443, in read
if not self._isOpen: raise portNotOpenError
ValueError: Attempting to use a port that is not open
If un-commented the line "neo.open()" it is throwing another error:
connected to: /dev/ttyUSB0
Traceback (most recent call last):
File "pyusb.py", line 13, in <module>
neo.open() #opens port
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 271, in open
raise SerialException("Port is already open.")
serial.serialutil.SerialException: Port is already open.
I have looked at the similar issue here. But there is a issue with overriding of "serial.Serial()" method. I am unable to identify exactly what is the wrong with code above. Can anyone help me with, what I am doing wrong there?
If you have a look at the docs for pyserial, http://pyserial.readthedocs.io/en/latest/shortintro.html#readline it says you must specify a timeout when using readline(). This is because readline() waits for a EOL character at the end of each transmission. Try increasing the timeout to 1 using something like this:
import serial
neo = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
print("connected to: " + neo.portstr)
while True:
dataline = neo.readline();
print dataline
I have been trying to connect my hc 05 bt module with my laptop and to achieve a communication with Idle.
At first i connected my bt device manually from laptop's bluetooth settings and then I was able to get the module's bt address by using the following code:
import bluetooth
target_name = "HC-05"
target_address = None
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print "found target bluetooth device with address ", target_address
else:
print "could not find target bluetooth device nearby"
I received the bluetooth address as "98:D3:31:70:7D:2D"
Now i wrote the following code to connect HC-05 via python..
bd_addr = "98:D3:31:70:7D:2D"
port = 20
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("1")
sock.close()
Now i browse through my laptop's bluetooth comm port settings and found 2 things
1st...
COM19
Direction--- Incoming...
Name--- HC-05
2nd...
COM20
Direction--- Outgoing
...
Name--- HC-05 'Dev B'
Hence I chose COM20 as I want to transmit data from Python.
When I run this code i get error:
Traceback (most recent call last):
File "C:\Python27\TestBluetooth.py", line 26, in <module>
sock.connect((bd_addr, port))
File "C:\Python27\lib\site-packages\bluetooth\msbt.py", line 72, in connect
bt.connect (self._sockfd, addr, port)
IOError: A socket operation failed because the destination host was down.
I tried both COM19 and COM20 but got the same error.
My Bluetooth is connected via TX pin to arduino's RX pin and the arduino is connected to my PC so there is no error of COM port sharing that occurs sometimes.
Also when I connect my bluetooth module with laptop bluetooth and open Bluetooth Serial terminal and transmit data from there it works fine.
So there's some problem in my understanding and writing of Python code.
Please help...
I am trying to implement a simple ftp with sockets using C (server side) and Python (client side). When the server code is compiled and run, the user enters a port number. The client then enters "localhost " when compiling. For some reason I am getting [Errno 111] on the client side when I run the code. It is saying that the issue is with my client.connect statement. I have tried using multiple different port numbers and it throws this same error:
flip1 ~/FTPClient 54% python ftpclientNew.py localhost 2500
Traceback (most recent call last):
File "ftpclientNew.py", line 86, in <module>
main()
File "ftpclientNew.py", line 27, in main
if client.connect((serverName, portNumber)) == None:
File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused
Another weird thing is that this connection error was not happening when I ran this same code a few days ago. Has anyone experienced a problem like this? Any idea what might be causing this? Thanks!
Here is the client code:
import sys, posix, string
from socket import *
def main():
if len(sys.argv) < 3:
print "\nFormat: 'localhost' <port number>!\n"
return 0
buffer = ""
bufferSize = 500
serverName = "localhost"
fileBuffer = [10000]
if sys.argv[1] != serverName:
print "Incorrect Server Name! \n"
return 0
portNumber = int(sys.argv[2])
client = socket(AF_INET, SOCK_STREAM)
if client < 0:
print "Error Creating Socket!! \n"
return 0
if client.connect((serverName, portNumber)) == None:
print "Client Socket Created...\n"
print "Connecting to the server...\n"
print "Connected!\n"
##clientName = raw_input("Enter a file name: ")
Sometimes localhost isn't working on host
Change this
serverName = 127.0.0.1
Try to change the serverName variable to 127.0.0.1.