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
(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>
i don't want to use a library please that is the requirement i have to work with raw sockets
Is this a homework/assignment in which case it should be in Homeworks
For such a simple app you could try out
bottle.py too.
(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.
(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.
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()