我正在开发一个读取端口并打印答案的python脚本。我们的想法是从gps获取数据。
这是我正在使用的代码。
#!/usr/bin/env python
import socket
host = '' # your IP address
port = 5000 # pick a port you want to listen on.
backlog = 5 # number of clients you could handle at once
size = 1024 # max size of data packet
# create socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to host / port:
s.bind((host,port))
# start listening:
s.listen(backlog)
print "listening on: " + str(port)
# stay in this loop until ctrl-c is typed at console:
while 1:
client, address = s.accept()
data = client.recv(size)
if data:
print data # echo data to console
client.send(data) # echo data to client (may confuse your application, great for browser)
client.close()
现在我可以通过telnet连接并发送消息,我将在服务器中获取输出。我也可以通过http:// [ip]:[port]连接并获取输出。但我无法从GPS获取任何信息,虽然它是在线的,我已经检查了配置。我也试过其他设备。没运气。
我的问题是:我在这里遗漏了什么吗?
答案 0 :(得分:0)
需要导入线程。
您需要s.
来编写数据,def
来运行脚本。
代码应该是这样的:
from threading import *
class client(thread):
def __init__(self, socket, address):
Thread.__init__(self)
s.sock = socket
s.addr = address
s.start()
def run(self)
while 1:
data = s.client.recv(size)
print ('send data:' data)
self.sock.send(b'Sending')
serversocket.listen(5)
print ('server start listing')
while 1:
clientsocket, address = serversocket.accept()
client(clientsocket, address)