无法通过 wifi 连接连接到套接字
Unable to connect to socket over wifi connection
我无法连接到 Raspberry Pi 3 B+ 上的套接字。这就是我所做的
- 已安装 dnsmasq 和 hostapd 并配置
- 创建了一个访问点并将静态 ip 分配给 wlano 作为 192.168.4.1,而不桥接到 lan
- 启动了一个 python 脚本来侦听端口 8888(正在成功等待连接)
- 创建了一个 Android 应用程序以连接到接入点并通过套接字将消息发送到端口 8888 上的 192.168.4.1
当我尝试使用 wlan0 静态 ip,192.168.4.1 连接到套接字时,出现未知主机异常。 python 脚本将套接字 ip 打印为 127.0.1.1 我如何 运行 Python 脚本来监听 wlan0 IP 而不是 127.0.1.1 这是我的 Python 脚本我从网上得到的
import socket
import sys
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
#Sending message to connected client
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
data = conn.recv(1024)
reply = 'OK...' + data
if not data:
break
conn.sendall(reply)
#came out of loop
conn.close()
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))
s.close()
我之前没有做过任何Python编程。所以我必须依赖脚本,它工作正常。
设置
HOST = "192.168.4.1"
在代码中,它应该可以工作。
我无法连接到 Raspberry Pi 3 B+ 上的套接字。这就是我所做的
- 已安装 dnsmasq 和 hostapd 并配置
- 创建了一个访问点并将静态 ip 分配给 wlano 作为 192.168.4.1,而不桥接到 lan
- 启动了一个 python 脚本来侦听端口 8888(正在成功等待连接)
- 创建了一个 Android 应用程序以连接到接入点并通过套接字将消息发送到端口 8888 上的 192.168.4.1
当我尝试使用 wlan0 静态 ip,192.168.4.1 连接到套接字时,出现未知主机异常。 python 脚本将套接字 ip 打印为 127.0.1.1 我如何 运行 Python 脚本来监听 wlan0 IP 而不是 127.0.1.1 这是我的 Python 脚本我从网上得到的
import socket
import sys
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
#Sending message to connected client
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
data = conn.recv(1024)
reply = 'OK...' + data
if not data:
break
conn.sendall(reply)
#came out of loop
conn.close()
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))
s.close()
我之前没有做过任何Python编程。所以我必须依赖脚本,它工作正常。
设置
HOST = "192.168.4.1"
在代码中,它应该可以工作。