Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Low Level Request Handling
#1
Hello all,
I'm new to Python and try to find out how useful it is for web programming.

I searched the web, but I couldn't find an answer to how to send data from my browser to
my Python program on the server, without using frameworks.

Is there an eqiuvalent to the php GET and POST commands?

How do I send the input, generated by a HTML form to a Python Program on my server?
Thank you!
Reply
#2
Hello! The data is just data. Your Python program has to listen for connection and get the data. Doesn't matter if it comes from a web browser or another program. Except you want some kind of validation.

I have to ask, what do you mean when you say "without framework"? There are 3-th party libraries which can save your time. Of course socket module is always here if you want to play with it. Along with asyncio. The socket connection is blocking.
Simplest example

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 52555
s.bind(0.0.0.0, port)

while True:
    c, address = s.accept()
    c.recv(1024)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Mar-14-2017, 10:05 AM)c0da Wrote: Hello all,
I'm new to Python and try to find out how useful it is for web programming.

I searched the web, but I couldn't find an answer to how to send data from my browser to
my Python program on the server, without using frameworks.

Is there an eqiuvalent to the php GET and POST commands?

How do I send the input, generated by a HTML form to a Python Program on my server?
Thank you!

GET/POST are faily high level commands. In Python see the requests module (non-standard, but very popular). Otherwise use the urllib or urllib2 modules.

Typical use of requests:
import json,requests

headers={'Content-type':'application/json'}
auth=('someid', 'somepwd')
parameters={"foo":"bar"} // as a python dictionary
url="http://someserver/somerequest/"

req=requests.post(url,auth=auth,headers=headers,data=json.dumps(parameters))
jsonresponse=req.json()
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
You don't need import json when you are using Requests Ofnuts.
Json decoder is build into Requests.
Quote:I'm new to Python and try to find out how useful it is for web programming.
For web programming you need to be using a Framework/Micro framework(this is close to PHP).
This make it lot easier to make a web app/site.

Use Flask(Micro framework) which is a layer over Raw WSGI.
Quote:Is there an eqiuvalent to the php GET and POST commands?
As i simple GET method.
Server:
from flask import Flask, request
app = Flask(__name__)

@app.route('/hello')
def api_hello():
    if 'name' in request.args:
        return 'Hello {}'.format(request.args['name'])
    else:
        return 'Hello John Doe'

if __name__ == '__main__':
    app.run()
Now use curl to do a GET requests:
E:\1py_div\flask_simple
λ curl http://127.0.0.1:5000/hello
Hello John Doe
E:\1py_div\flask_simple
λ curl http://127.0.0.1:5000/hello?name=Tom
Hello Tom
You see it simple and low level to do a GET requests.
Post it's just in similar manner.

I have some tutorials here you can look.
Reply
#5
(Mar-14-2017, 03:00 PM)snippsat Wrote: You don't need import json when you are using Requests Ofnuts.
Json decoder is build into Requests.

In the recent ones yes, but the one from by 'buntu repo hasn't got it yet.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
Quote:In the recent ones yes, but the one from by 'buntu repo hasn't got it yet.
Should really not use a repo with old Requests at all,
it's updated frequently and many of updates are security related.
Reply
#7
Hm! All @c0da wants is to handle get and post requests. I thing twisted or tornado could be a better option.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Mar-14-2017, 06:29 PM)wavic Wrote: Hm! All @c0da wants is to handle get and post requests. I thing twisted or tornado could be a better option.
No,that's not what i understand for his question at all.
When have Twisted or Tornado been a okay answer to do simple GET and POST requests?
Reply
#9
He wants to send data from a web browser to some program: "how to send data from my browser to my Python program on the server,". Obviously, he has the web page with some form and needs to handle the post request sent by the browser
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Have to wait for c0da to clear up his intentions(which is not clear) to give a better answer,
no point that we discuss this any further.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python handling Apache Request harzsr 3 3,808 Nov-16-2018, 04:36 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

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