Python Forum

Full Version: Low Level Request Handling
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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!
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)
(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()
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.
(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.
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.
Hm! All @c0da wants is to handle get and post requests. I thing twisted or tornado could be a better option.
(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?
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
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.
Pages: 1 2