无法通过wifi连接连接到套接字

时间:2019-02-13 18:57:10

标签: android python websocket raspberry-pi access-point

我无法连接到Raspberry Pi 3 B +上的插槽。这就是我所做的

  1. 安装了dnsmasq和hostapd并进行了配置
  2. 创建了一个接入点,并将静态IP分配给wlano为192.168.4.1,而没有桥接到lan
  3. 启动python脚本以侦听端口8888(已成功等待连接)
  4. 创建了一个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编程。因此,我必须依靠脚本,并且脚本运行良好。

1 个答案:

答案 0 :(得分:1)

设置

HOST = "192.168.4.1"

在代码中,它应该可以工作。