English 中文(简体)
29. Joe s Erlang网络插图
原标题:Python example of Joe s Erlang websocket example

I ve Just is working through the erlang websockets example from Joe Armstrong s blog I m仍然很新到lang兰,因此,我决定在 p写一个简单的服务器,帮助教我了解网上图书目录(希望通过解释《职业法典》来修饰一些lang)。 我有两个问题:

(1) 数据 我从网页上接见了最后的char。 这在地兰版上是一纸空文,我可以发现它来自的地方。 固定 这是因为在图f-8和I wasnt编码的插图中脱节。

2) 我似乎正在从服务器上(通过网站目录)发送数据,可以通过审查多少客户来确认这些数据。 发出。 但网页上没有任何内容。 Fixed, I wasn t encoding the string appropriate

I ve put all the Code here。 在这里,一米失踪的神秘说法

import threading
import socket

def start_server():
    tick = 0
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(( localhost , 1234))
    sock.listen(100)
    while True:
        print  listening... 
        csock, address = sock.accept()
        tick+=1
        print  connection!  
        handshake(csock, tick)
        print  handshaken 
        while True:
            interact(csock, tick)
            tick+=1

def handshake(client, tick):
    our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake
"+"Upgrade:     WebSocket
"+"Connection: Upgrade
"+"WebSocket-Origin:     http://localhost:8888
"+"WebSocket-Location: "+" ws://localhost:1234/websession

"
    shake = client.recv(255)
    print shake
    client.send(our_handshake)

def interact(client, tick):
    data = client.recv(255)
    print  got:%s  %(data)
    client.send("clock ! tick%d
" % (tick))
    client.send("out ! recv
")

if __name__ ==  __main__ :
    start_server()

对于那些 t不 through的人来说,你只是需要做互动。 html通过一个网络服务器,然后开始服务器(该代码假定网络服务器在当地运行:8888)

最佳回答
问题回答

活动手册有网页支持,星号是使用带有金字塔网框架的网页材料包:





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...