【问题标题】:How to convert terminal window chat application (built using socket programming, written in python) into a web application?如何将终端窗口聊天应用程序(使用套接字编程构建,用 python 编写)转换为 Web 应用程序?
【发布时间】:2014-04-25 18:18:46
【问题描述】:

所以,我编写了这个在终端窗口中运行良好的聊天应用程序: GitHub Source

现在我想要转换为网络聊天应用程序,以便我的朋友可以从他们的网络连接/聊天/测试它。但是,我不知道如何进行!

请帮助我。建议我可以使用哪些技术使其在网站上可用?

【问题讨论】:

    标签: python sockets web-applications chat


    【解决方案1】:

    您似乎已经编写了一个 Python 服务器来处理您的 Python 聊天客户端,并且您希望将其扩展到 Web 客户端。

    我建议使用 PubNub 等实时网络在您的聊天客户端和服务器之间中继数据。使用实时网络意味着您可以花更少的时间来担心并发等低级套接字问题,并有更多的时间来构建您的应用程序。

    对于 PubNub,Python SDK 将允许您的服务器订阅聊天频道,而JavaScript SDK 将帮助基于 Web 的客户端。您可以使用这篇博文中的代码构建一个简单的基于 JavaScript 的 Web 客户端:Build Real-Time Chat Apps in 10 Lines of Code

    Enter Chat and press enter
    <div><input id=input placeholder=you-chat-here /></div>
    
    Chat Output
    <div id=box></div>
    
    <script src=http://cdn.pubnub.com/pubnub.min.js></script>
    <script>(function(){
      var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chat';
      PUBNUB.subscribe({
        channel : channel,
        callback : function(text) { box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML }
      });
      PUBNUB.bind( 'keyup', input, function(e) {
        (e.keyCode || e.charCode) === 13 && PUBNUB.publish({
          channel : channel, message : input.value, x : (input.value='')
        })
      } )
    })()</script>
    

    然后,在您的 Python 服务器上,您可以订阅相同的聊天频道:

    # Listen for Messages *BLOCKING*
    def receive(message) :
      broadcast_data(message) #This is using your Python function from your github link
      return True
    
    pubnub.subscribe({
      'channel'  : 'chat',
      'callback' : receive 
    })
    

    让我知道这是否适合您。祝你好运!

    【讨论】:

    • 如果您喜欢冒险,请发送电子邮件至 support@pubnub.com 以提前访问我们新的 3.5 beta PubNub Python 客户端!
    • 非常感谢!我会试一试,如果可行,我会接受答案! :)
    猜你喜欢
    • 2016-04-30
    • 1970-01-01
    • 2011-07-30
    • 2020-10-25
    • 2018-01-29
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多