Python Forum
socket programming (browser)
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket programming (browser)
#1
i would like to make a small application to write a name and then display in reverse order but this should use raw sockets in python making the input and output in a browser i don't even know where to begin cant find any good references if some one could just start me off[Image: qZGMM.png]
Reply
#2
(Dec-10-2018, 08:11 PM)kunz Wrote: but this should use raw sockets in python making the input and output in a browser
For web-development raw sockect would be a nightmare to work with.
A short history CGI was bad for Python regarding the future of web-development in Python,
and all Python solution's was work out WSGI(PEP 3333).
Why is WSGI necessary?
So to day all web-framework is build on this standard eg Flask,Django...ect.

Example with Flask.
form_test\
  |-- app.py
templates\
  |-- index.htm
app.py:
from flask import Flask, render_template, request, redirect, url_for
 
app = Flask(__name__)
@app.route('/')
def index():
   return render_template("index.html")

@app.route('/text', methods=['GET', 'POST'])
def text(comments=[]):
    if request.method == "GET":
        return render_template("index.html", comments=comments)  
    value = request.form["text_input"]     
    comments.append(value[::-1] )  
    return redirect(url_for('text'))

if __name__ == '__main__':
   app.run(debug=True)
index.html:
<!doctype html>
<html>
<head>
  <title>Some title</title>
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style1.css') }}" />
</head>
<body>
  <h2>Form</h2>
  <form action="/text" method="post">
    Input text:<br>
    <input type="text" name="text_input" value="">
    <br>
    <input type="submit" value="Submit">
  </form>
  <div id='foo'>
    <br>
    {% for comment in comments %}
    <div class="row">
      {{ comment }} 
    </div>
    {% endfor %}
  </div>
</body>
</html>
[Image: 4nQ7IS.jpg]
Reply
#3
i don't want to use a library please that is the requirement i have to work with raw sockets
Reply
#4
Is this a homework/assignment in which case it should be in Homeworks
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Can look at this for a start Let’s Build A Web Server. Part 1.
If it homework it's not an easy task.
Reply
#6
For such a simple app you could try out bottle.py too.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Dec-11-2018, 09:27 AM)wavic Wrote: For such a simple app you could try out bottle.py too.
If he can not Flask,then neither can he use bottle.
With only use of raw socket,so is this not an easy task at all.
Reply
#8
(Dec-11-2018, 10:00 AM)snippsat Wrote:
(Dec-11-2018, 09:27 AM)wavic Wrote: For such a simple app you could try out bottle.py too.
If he can not Flask,then neither can he use bottle.
With only use of raw socket,so is this not an easy task at all.
Oh, I see it now. Well, he is likely to write a new web framework? Not an easy task.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
import socket


def client_program():
    host = socket.gethostname()  # as both code is running on same pc
    port = 5000  # socket server port number

    client_socket = socket.socket()  # instantiate
    client_socket.connect((host, port))  # connect to the server

    message = input(" -> ")  # take input

    while message.lower().strip() != 'bye':
        client_socket.send(message.encode())  # send message
        data = client_socket.recv(1024).decode()  # receive response

        print('Received from server: ' + data)  # show in terminal

        message = input(" -> ")  # again take input

    client_socket.close()  # close the connection


if __name__ == '__main__':
    client_program()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket programming ConnectionRefusedError error srm 3 12,794 May-16-2019, 08:07 PM
Last Post: LavaCreeperKing
  Python Socket programming with packets sourabhjaiswal92 1 4,091 Sep-18-2018, 06:24 AM
Last Post: martingever
  socket programming user2103 2 3,579 Dec-19-2017, 11:52 AM
Last Post: user2103

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020