Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 576289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:01:54+00:00 2026-05-13T14:01:54+00:00

I’ve just been working through the erlang websockets example from Joe Armstrong’s blog I’m

  • 0

I’ve just been working through the erlang websockets example from Joe Armstrong’s blog I’m still quite new to erlang so I decided to write a simple server in python that would help teach me about websockets (and hopefully some erlang by interpreting joe’s code). I’m having two issues:

1) Data I receive from the page includes a ‘ÿ’ as the last char. This doesn’t appear in the erlang version and I can’t work out where it’s coming from Fixed – This was because the strings where encoded in utf-8 and I wasn’t decoding them

2) I seem to be sending data from the server (through the websocket) – which can be confirmed by looking at how many bytes client.send() makes. But nothing is appearing on the page. Fixed, I wasn’t encoding the string correctly

I’ve put all the code here. Here’s my python version incase i’m missing anything obvious

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\r\n"+"Upgrade:     WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin:     http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n"
    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\r" % (tick))
    client.send("out ! recv\r")

if __name__ == '__main__':
    start_server()

For those who haven’t run through joe’s example but still want to help, you just need to serve up interact.html through a web server and then start your server (The code assumes the webserver is running on localhost:8888)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:01:54+00:00Added an answer on May 13, 2026 at 2:01 pm

    For those who are interested this was the solution

    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 send_data(client, str_):
        #_write(request, '\x00' + message.encode('utf-8') + '\xff')
        str_ = '\x00' + str_.encode('utf-8') + '\xff'
        return client.send(str_)
    def recv_data(client, count):
        data = client.recv(count)    
        return data.decode('utf-8', 'ignore')
    
    def handshake(client, tick):
        our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+"Upgrade:     WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin: http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n"
        shake = recv_data(client, 255)
        print shake
        #We want to send this without any encoding
        client.send(our_handshake)
             
    def interact(client, tick):
        data = recv_data(client, 255)
        print 'got:%s' %(data)
        send_data(client, "clock ! tick%d" % (tick))
        send_data(client, "out ! %s" %(data))
    
    if __name__ == '__main__':
        start_server()
    

    Edit for liwp’s request:

    You can view a diff of the file here. Essentially my problem was the way I was decoding / encoding strings prior to sending / receiving. There’s a websocket module being worked on for Apache on google code which I used to find out where I was going wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.