Jen Scott
Jen Scott

Reputation: 679

Python execute HTTP commands

To be brief, I've basically found that executing certain strings in the address bar in my browser causes good things to happen for me. For instance: http://myip:myport/?CreateOrder=Create+New+Order creates a new order in my database.

I've used python to set the variables I want passed into that string, but don't know the correct vocab to be able to figure exactly how to get python to execute that string as if it were a browser.

Sorry for being ignorant. I'd appreciate any help

Upvotes: 1

Views: 7739

Answers (2)

Acorn
Acorn

Reputation: 50507

You should check out the Requests module.

import requests

r = requests.get('http://httpbin.org/get', params={'foo': 'bar'})

print r.content

(httpbin.org is a nice service for debugging HTTP requests)

Result:

{
  "url": "http://httpbin.org/get?foo=bar",
  "headers": {
    "Content-Length": "",
    "Accept-Encoding": "identity, deflate, compress, gzip",
    "X-Forwarded-Port": "80",
    "Host": "httpbin.org",
    "Accept": "*/*",
    "User-Agent": "python-requests/0.8.3",
    "Connection": "keep-alive",
    "Content-Type": ""
  },
  "args": {
    "foo": "bar"
  },
  "origin": "46.64.26.143"
}

Upvotes: 1

Tom Neyland
Tom Neyland

Reputation: 6968

While I am not sure that I entirely understand the context of your question you can make URL requests in python using the urllib2 module, specifically look at the urlopen function.

Here is an example of how you could make the request you showed:

import urllib2

url_response = urllib2.urlopen('http://myip:myport/?CreateOrder=Create+New+Order')

Upvotes: 2

Related Questions