I don't mean to sound harsh, but being new to a language is no excuse for not learning the language's syntax (quite on the contrary). This line:
data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="
is obviously broken and raises a SyntaxError:
bruno@bigb:~/Work/playground$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
>>> data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="
File "<stdin>", line 1
data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="
^
SyntaxError: invalid syntax
>>>
In this statement the rhs expression actually begins with :
"POSTDATA=FromDate=" which is a legal literal string
i&ToDate which is parsed as "i" (identifier) "&" (operator) "ToDate" (identifier)
The mere juxtaposition of a literal string and an identifier (without an operator) is actually illegal:
bruno@bigb:~/Work/playground$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
>>> i = 42
>>> "foo" i
File "<stdin>", line 1
"foo" i
^
SyntaxError: invalid syntax
>>>
Obviously what you want here is string concatenation, which is expressed by the add ("+") operator, so it should read:
"POSTDATA=FromDate=" + i
Then since "&ToDate" is supposed to be a string literal instead of an operator and a variable you'd have to quote it:
"POSTDATA=FromDate=" + i + "&ToDate="
Then concatenate the current date again:
"POSTDATA=FromDate=" + i + "&ToDate=" + i + "etc..."
Now in your code i (not how I would have named a date BTW but anyway) is a datetime object, not a string, so now you'll get a TypeError because you cannot concatenate a string with anything else than a string (hopefully - it wouldn't make any sense).
FWIW what you want here is not a datetime object but the textual ("string") representation of the date in the "YYYY-MM-DD" format. You can get this from the datetime object using it's strftime() method:
today = datetime.datetime.now().strftime("%Y-%m-%d)
Now you have a string that you can concatenate:
data = "POSTDATA=FromDate=" + today + "&ToDate=" + today + "etc..."
This being said:
- this kind of operation is usually done using string formatting
- and as Ekrem Dogan mentionned, the simplest solution here is to use a higher-level package that will take care of all the boring details of HTTP requests -
requests being the de facto standard.