Passing parameters to a CGI program using the URL (python) - python

As the title suggests, I'm trying to pass parameters into my cgi script so that when you type in (for example): www.helloworld.com/cgi-bin/world.py?post=101, the script will display that post
I've tried the following:
link = 'test' % postNumber
link = cgi.FieldStorage()
id = link.getvalue('post')
print id
but the value of id is nothing. It's like it's not reading the link properly or something.
Please help!

How about:
id = link["post"].value
print id

Related

Python print values from json table with requests?

I just started python and can't figure out how I would do this.
So I want to make a tool that interacts with roblox.com.
I'm using requests to get something from a specific user.
r = requests.get("https://www.roblox.com/Groups/GetPrimaryGroupInfo.ashx?users=Shedletsky")
print r.text
returns
{"Shedletsky" : {"GroupId" : 2814397, "GroupName" : "Shedletsky Studios", "RoleSetName" : "Owner", "RoleSetRank" : 255}}
How do I interact with the json table to get only certain variables? e.g make it Print the GroupName only so in the end I would get something like:
Groupname: Shedletsky Studios
to extract your data from text json you need to convert it to json first, so do this
import json
r = requests.get("https://www.roblox.com/Groups/GetPrimaryGroupInfo.ashx?users=Shedletsky")
print r.text
data = json.load(r.text)
print data['GroupName']
this will get your work done
hope this helps :)

How to parse web elements into notepad using Python?

can anyone help me with "extracting" stuff from site using Python? Here is the info :
I have folder name with set of numbers (they are ID of item) and i have to use that ID for entering page and then "scrap" info from page to my notepad... It's like this : http://www.somesite.com/pic.mhtml?id=[ID]... I need to exctract picture link (picture link always have ID.jpg at the end of the file)from it and write it in notepad and then replace that txt name with name of the picture... Picture is always in title tags... Thanks in advance...
What you need is a data scraper - http://www.crummy.com/software/BeautifulSoup/ will help you pull data off of websites. You can then load that data into a variable, write it to a file, or do anything you normally do with data.
You could try parsing the html source for images.
Try something similar:
class Parser(object):
__rx = r'(url|src)="(http://www\.page\.com/path/?ID=\d*\.(jpeg|jpg|gif|png)'
def __crawl(self, url):
images = []
code = urllib.urlopen(url).read()
for line in code.split('\n'):
imagesearch = re.search(self.__rx, line)
if imagesearch:
image = '%s.%s' % (imagesearch.group(2), imagesearch.group(4))
images.append(image)
return images
it's untestet, you may want to check the regex

Using the Python GData API, cannot get editable video entry

I am having trouble getting a video entry which includes a link rel="edit". I need such an entry in order to be able to call DeleteVideoEntry(...) on it.
I am retrieving the video using GetYouTubeVideoEntry(youtube_id=XXXXXXX). My yt_service is initialized with a username, password, and a developer key. I use ProgrammaticLogin. This part seems to work fine. I use the same yt_service to upload said video earlier. Also, if I change the developer key to something bogus (during debugging) and try to authenticate, I get a 403 error. This leads me to believe that authentication works OK.
Needsless to say, the video entry retrieved with GetYouTubeVideoEntry(youtube_id=XXXXXXX) does not contain the edit link and I cannot use the entry in a DeleteVideoEntry(...) call.
Is there some special way to get a video entry which will contain a link element with a rel="edit"? Can anyone suggest some way to resolve my issue? Could this possibly be a bug?
Update:
For the records, when I tried getting the feed of all my uploads, and then looping through the video entries, the video entries do have an edit link. So using this works:
uri = 'http://gdata.youtube.com/feeds/api/users/%s/uploads' % username
feed = yt_service.GetYouTubeVideoFeed(uri)
for entry in feed.entry:
yt_service.DeleteVideoEntry(entry)
But this does not:
entry = yt_service.GetYouTubeVideoEntry(video_id = video.youtube_id)
yt_service.DeleteVideoEntry(entry)
Using the same yt_service.
I've just deleted youtube video using gdata and ProgrammaticLogin()
Here is some steps to reproduce:
import gdata.youtube.service
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = 'developer_key'
yt_service.email = 'email'
yt_service.password = 'password'
yt_service.ProgrammaticLogin()
# video_id should looks like 'iu6Gq-tUsTc'
uri = 'https://gdata.youtube.com/feeds/api/users/%s/uploads/%s' % (username, video_id)
entry = yt_service.GetYouTubeUserEntry(uri=uri)
response = yt_service.DeleteVideoEntry(entry)
print response # True
yt_service.GetYouTubeVideoFeed(uri) works because GetYouTubeVideoFeed doesn't check uri and just calls self.Get(uri, ...) but originaly, I think, it expected 'https://gdata.youtube.com/feeds/api/videos' uri.
vice versa yt_service.GetYouTubeVideoEntry() use YOUTUBE_VIDEO_URI = 'https://gdata.youtube.com/feeds/api/videos' but this entry doesn't contains rel="edit"
Hope that helps you out
You can view the HTTP headers of the generated requests by setting the debug flag to true. This is as simple as:
yt_service = gdata.youtube.service.YouTubeService()
yt_service.debug = True
You can read about this in the documentation here.

How to store data to datastore - AppEngine

I am new to Python & AppEngine.
I am trying to use Feedparser to cache a feed to a datastore.
My code is at http://pastebin.com/uWPdWUm2
For some reason it doesn't work - it does not add the data to the datastore.
Any ideas? I am stumped.
You just forgot to use parenthesis in your model declaration.
Your code:
class FeedEntry3(db.Model):
title = db.StringProperty
link = db.StringProperty
content = db.TextProperty
What it should be:
class FeedEntry3(db.Model):
title = db.StringProperty()
link = db.StringProperty()
content = db.TextProperty()
Are you sure you are getting the values correcty or at all from feed parser? Have you tried to log them. Also for purpose of discussion if you think x.put is not working then separate that out and test that only e.g.
x = FeedEntry3()
x.title = "test title"
x.link = "test link"
x.content = "test content"
x.put()
Have you tried that, does that work? if that works most probably you are not getting values from feedparser, debug and log that.

HTML forms not working with python

I've created a HTML page with forms, which takes a name and password and passes it to a Python Script which is supposed to print the persons name with a welcome message. However, after i POST the values, i'm just getting the Python code displayed in the browser and not the welcome message. I have stored the html file and python file in the cgi-bin folder under Apache 2.2. If i just run a simple hello world python script in the browser, the "Hello World" message is being displayed. I'm using WinXP, Python 2.5, Apache 2.2. the code that i'm trying to run is the following:
#!c:\python25\python.exe
import cgi
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
reshtml = """Content-Type: text/html\n
<html>
<head><title>Security Precaution</title></head>
<body>
"""
print reshtml
User = form['UserName'].value
Pass = form['PassWord'].value
if User == 'Gold' and Pass == 'finger':
print '<big><big>Welcome'
print 'mr. Goldfinger !</big></big><br>'
print '<br>'
else:
print 'Sorry, incorrect user name or password'
print '</body>'
print '</html>'
The answer to it might be very obvious, but its completely escaping me. I'm very new to Python so any help would be greatly appreciated.
Thanks.
This
i'm just getting the Python code
displayed in the browser
sounds like CGI handling with Apache and Python is not configured correctly.
You can narrow the test case by passing UserName and PassWord as GET parameters:
http://example.com/cgi-bin/my-script.py?UserName=Foo&PassWord=bar
What happens if you do this?
You may have to extract the field values like this
User = form.getfirst('UserName')
Pass = form.getfirst('PassWord')
I know, it's strange.

Categories

Resources