【发布时间】:2019-01-17 17:25:51
【问题描述】:
喂!实际上,我正在使用 python 语言通过使用以下模块 autobahn,扭曲连接到我的谷歌云 vm 实例。我的谷歌云 vm 实例可以完美地接收图像并将其发送回 python 客户端文件。
但是,当我们在 Flutter 上开发我们的 Android/IOS 应用程序时,我需要从 dart 调用我的 python 代码,以便我可以从 Flutter 应用程序调用我的 python 函数。但我无法找到任何方法来做到这一点。 这是我的客户端代码,它将连接到部署在 vm 实例上的服务器。
from autobahn.twisted.websocket import WebSocketClientProtocol, \
WebSocketClientFactory
import main
import jsonpickle
import base64
import matplotlib.pyplot as plt
import time
class MyClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def onOpen(self):
print("WebSocket connection open.")
def hello():
# opening the image file and encoding in base64
path = "/home/abdullah/Desktop/Clocktower_Panorama_20080622_20mb.jpg"
image = main.load_image(path)
encoded = jsonpickle.encode(image)
# printing the size of the encoded image which is sent
# print("Encoded size of the sent image: {0} bytes".format(len(encoded_string)))
# sending the encoded image
self.sendMessage(encoded.encode('utf-8'))
hello()
def onMessage(self, payload, isBinary):
print("Output processed image received")
output = main.image_post_process(payload)
plt.imshow(output)
plt.show()
print("Done")
reactor.stop()
# plt.show()
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
time1 = time.time()
import sys
from twisted.python import log
from twisted.internet import reactor
# log.startLogging(sys.stdout)
factory = WebSocketClientFactory()
factory.protocol = MyClientProtocol
reactor.connectTCP("34.73.158.146", 5903, factory)
# reactor.connectTCP("localhost", 9933, factory)
reactor.run()
time2 = time.time()
print("Total time is ", time2-time1)
请告诉我如何解决这个问题。谢谢
【问题讨论】:
-
没有您的一些示例代码,我们无法为您提供知情帮助。
-
无法理解“我需要将我的 python 代码解析为 dart”。这意味着什么。为什么不使用 HTTP 请求和 JSON 或类似的方式从 Flutter 与您的云服务器进行通信?
-
@JordanSinger 我添加了代码
-
@GünterZöchbauer 抱歉,我将其更改为“我需要从 dart 语言调用我的 python 代码”。是现在吗?
-
Flutter 没有很好的方法来调用它。最好在 Dart 中重新构建它。
标签: python parsing server dart flutter