据我所知,此应用程序将100%无法正常工作,但这仅用于测试,但是我期待浏览器拒绝连接以外的其他错误,我什至没有得到我的浏览器尝试连接的日志!因此,它使我想起了它,然后才开始工作,我需要看看为什么它会被拒绝(我正在对此做实验以尝试http!)注意:我使用的套接字不是任何HTTP库,并且此问题仅存在于浏览器中,而不存在于客户端上我写的应用程序,所以其他答案对我没有帮助,这是我的代码:
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 1234 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send(open("GPIOWEB/index.html").read())
print c.recv(1024)
c.close() # Close the connection
客户 :
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 1234 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
您可以通过浏览器访问您的server.py程序,但你需要告诉浏览器(所以它可以告诉OS)如何你希望那里。
当你做
host = socket.gethostname() # Get local machine name
s.bind((host, 1234)) # Bind to the port
您正在绑定到特定接口。
print(host)
在其中添加一个,以查看您要绑定到的接口。然后,在浏览器中,输入<host>:1234
地址-<host>
打印的位置。
您的浏览器将显示的内容,GPIOWEB/index.html
而server.py程序将显示如下内容:
从('127.0.0.1',63358)获得连接 GET / HTTP / 1.1 主机:localhost:1234 连接:保持活动状态 接受:text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,* / *; q = 0.8 用户代理:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 41.0.2272.89 Safari / 537.36 接受编码:gzip,deflate,sdch 接受语言:en-US,en; q = 0.8
或者,使用以下命令绑定到所有可用接口:
port = 1234 # Reserve a port for your service.
s.bind(('', port)) # Bind to the port
(注意''
用于主机)
现在,您应该能够通过各种地址与程序进行通信,例如:
localhost:1234
127.0.0.1:1234
<Your LAN IP>:1234
其中一些可能取决于您的防火墙设置,如果您没有获得预期的结果,则可以考虑暂时禁用它,然后适当地更新其配置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句