Coder Social home page Coder Social logo

httpsocket's Introduction

HTTPSOCKET

Using Python or C++, provide a single file program that:

a. Accepts an HTTP connection b. Parses the HTTP connection data c. If the HTTP requests the enpoint '/ws', the program will upgrade the connection to WebSocket d. Send the following JSON through the WebSocket '{ "status" : "success" }'

Initial

This projects it`s developing using a python-3.5.2

Create a socket class SocketHttp, passing a full url ex ws://echo.websocket.org/ for the socket connection:

>>> conn = SocketHttp('ws://echo.websocket.org/')

HTTP

For make a http connection, uses the method http:

>>> conn = SocketHttp("https://restcountries.eu/rest/v2/region/europe")
>>> conn.http()

To see the headers:

>>> print(conn.headers)
{'Date': 'Thu, 14 Sep 2017 03:40:52 GMT', 'Server': 'Apache/2.4.25 (Debian)', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Accept, X-Requested-With', 'Cache-Control': 'public, max-age=86400', 'Content-Type': 'application/json;charset=utf-8', 'Transfer-Encoding': 'chunked'}

To see the status_code:

>>> print(conn.status_code)
200

WebSocket

For check, if the url is available to upgrade, just call the method assert_websocket:

>>> conn = SocketHttp("https://restcountries.eu/rest/v2/region/europe")
>>> conn.assert_websocket()
False
>>> conn = SocketHttp("ws://echo.websocket.org/")
True

For make a upgrade connection, uses the method upgrade:

>>> conn = SocketHttp("ws://echo.websocket.org/")
>>> conn.upgrade()
>>> conn.headers
{'Connection': 'Upgrade',
 'Date': 'Thu, 14 Sep 2017 03:55:02 GMT',
 'Sec-WebSocket-Accept': 'f3GqT8GUCucuiz5mLwm9q+FFDmM=',
 'Server': 'Kaazing Gateway',
 'Upgrade': 'websocket'}

To send data, just call the method send:

>>> conn.send('{ "status" : "success" }')

Close

To close the socket, just:

>>> conn.close()

httpsocket's People

Contributors

guilhermetavares avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.