Hello.

I have a python script to post news a website but i want to know how i can pass variables from php to the python script and run the script from php.

Python script: (I didn't create this, was done by a friend)

import urllib2, urllib

USER = '' # Put your username within the quotes
PASS = '' # Put your password within the quotes
URL = 'http://steamcommunity.com/groups/<groupid>/announcements' # Change this to the url of the page where you post an announcement on your steam group

TITLE = '' # News header
MESSAGE = '' # news body

# build opener with HTTPCookieProcessor
o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
urllib2.install_opener( o )

# Build the url for login
p = urllib.urlencode( { 'action' : 'doLogin', 'steamAccountName': USER, 'steamPassword': PASS } )

# perform login with params
f = o.open( 'https://steamcommunity.com/',  p )
data = f.read()
f.close()

# second request should automatically pass back any
# cookies received during login... thanks to the HTTPCookieProcessor
# Send the post
p = urllib.urlencode( { 'action' : 'post', 'headline' : TITLE, 'body' : MESSAGE } )
f = o.open( URL, p )
data = f.read()
f.close()

Any help is much appreciated :)
Thanks

I would think that the best way would be to use a form with hidden variables for the parameters. The other option would be a link with the variables as parameters (<a href=xxx.pyt?a=xxx&b=yyyy...). I'm not able to advise you on how to access these in the Python script but it should be pretty straightforward. If you need advice on that, I suggest that you go over to the Python forum. The other option would be to convert this short python script to php and make it less complicated.

You can make the python script to accept argument from php. Something like this:

#!/usr/bin/env python
import urllib2, urllib, sys

USER = '' # Put your username within the quotes
PASS = '' # Put your password within the quotes
URL = 'http://steamcommunity.com/groups/<groupid>/announcements' # Change this to the url of the page where you post an announcement on your steam group
count = 0
for arg in sys.argv:
	if arg == "-t":
		TITLE = sys.argv[count+1] # News header
	elif arg == "-m":
		MESSAGE = sys.argv[count+1] # news body
	count+=1

# build opener with HTTPCookieProcessor
o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
urllib2.install_opener( o )

# Build the url for login
p = urllib.urlencode( { 'action' : 'doLogin', 'steamAccountName': USER, 'steamPassword': PASS } )

# perform login with params
f = o.open( 'https://steamcommunity.com/',  p )
data = f.read()
f.close()

# second request should automatically pass back any
# cookies received during login... thanks to the HTTPCookieProcessor
# Send the post
p = urllib.urlencode( { 'action' : 'post', 'headline' : TITLE, 'body' : MESSAGE } )
f = o.open( URL, p )
data = f.read()
f.close()

And for the php:

<?php
$title = "Some title";
$message = "some message";
$command = "/path/to/script/primes.py -t $title -m $message";
exec($command);
?>

or as suggested by chrishea, you can also use php to post the news.

Thank you Chrishea and johnburn for your quick response. I will try these methods later when i have time.

Thanks a lot johnburn.It works fine.You are great.