python:测试GPSd套接字(2947)永远无法连接second+tim

2024-05-14 15:20:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是检查GPSd是否正在2947端口上运行,如果它在x秒内再次检查。如果它没有启动GPSd,那么在X秒内再次检查。在

我的问题是它永远不会在第二次++时间通过/连接。在

我的代码:

import socket
import os
import time

#Simply change the host and port values
host = 'localhost'
port = 2947

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

while 1:
 try:
  s.connect((host, port))
  s.shutdown(2)
  #s.close() DIDN'T WORK
  print "Success connecting to "
  print host + " on port: " + str(port)
  time.sleep(20)
 except:
  print "Cannot connect to "
  #os.system("sudo killall gpsd")
  #os.system("sudo gpsd /dev/ttyAMA0 -n -F /var/run/gpsd.sock")
  print host + " on port: " + str(port)
  #os.system("sudo killall gpsd")
  #os.system("sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock")
  time.sleep(3)

我得到的是:

成功连接到
端口上的本地主机:2947
无法连接到
端口上的本地主机:2947
无法连接到
端口上的本地主机:2947

我已经想尽一切办法,但因为我不明白是什么导致了这个问题,这对我没有多大帮助。在


Tags: to端口importhosttimeosonport
2条回答

我认为你应该在你关闭后再打电话给关闭。在

我相信您正在(重新)调用20秒前关闭的同一个套接字上的connect。遗憾的是,不能这么做。在

至于文件插座关闭说:

"Lasciate ogne speranza, voi ch'intrate"

这大致可以理解为:

socket.shutdown(how)

     Shut down one or both halves of the connection. If how is SHUT_RD,
further receives are disallowed. If how is SHUT_WR, further sends are
disallowed. If how is SHUT_RDWR, further sends and receives are disallowed.

'插座.关闭()`比那更重要:

^{pr2}$

您可以将s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)移到try语句之后的循环中……我只是不知道为什么要朝这个方向前进。在

相关问题 更多 >

    热门问题