現在作っている制作物で、raspberry pi側をクライアントandroid側をサーバとしてソケット通信するプログラムを作っています。
汎用性を出すためにサーバー側のIPをファイルに書き込みそこから出た値でconnect使用としているのですが、name or service not known と怒られてしまいます。
client.connect(("xxx.xxx.xxx.xxx",port))のようにIPアドレス部分をダブルクォートで定数として入れたら正常に機能してandroid側に自分のIPとtrueを送ってくれます。
printして見た目的に比較してもtypeを出力しても同じstr型です。
ファイルから読むのと書き込むのと何が違うのでしょうか。
ご回答よろしくお願いします。
python
1import socket 2import time 3import netifaces 4def getAddress(path:str): 5 ''' 6 param path {str} ip wrote file path 7 descreption require write ip only 8 ''' 9 ip="" 10 tmp='' 11 with open(path,"r") as file: 12 ip=file.read() 13 return ip 14 15 16def connect(address:str,port:int): 17 ''' 18 param address {str} connect destination 19 param port {int} connect port 20 ''' 21 import traceback 22 try: 23 with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as client: 24 print(address,end="") 25 client.connect((address,port)) 26 stopByte=client.recv(1024) 27 sendData=b'true'+stopByte+netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr'].encode('utf-8')+stopByte 28 client.sendall(sendData) 29 res="" 30 while res!='ack': 31 res=client.recv(1024).decode() 32 time.sleep(5) 33 except: 34 traceback.print_exc()
error
1Traceback (most recent call last): 2 File "/home/pi/python/clientToAndroid.py", line 25, in connect 3 client.connect((address,port)) 4socket.gaierror: [Errno -2] Name or service not known
wifi1.ip
1//wifi1.ip 2xxx.xxx.xxx.xxx 3//ip定数 改行なし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/17 05:47