sending data using post in python to php with variable
Question: I have this script and I want to replace the date `2015-05-01` (from and to)
with the current date. I am thinking of using **import datetime** and
something like this (I am new to python):

    
    
    from StringIO import StringIO
    import urllib
    import urllib2
    
    url = 'http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw'
    data = "POSTDATA=FromDate=2015-05-01&ToDate=2015-06-01&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken= " 
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    
    the_page = response.read()
    print the_page
    

**New script**

    
    
    from StringIO import StringIO
    import urllib
    import urllib2
    import datetime
    
    i = datetime.datetime.now()  // gets the date
    url = 'http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw'
    data = "POSTDATA=FromDate="i&ToDate="i"&Countrycode=&InsertedSinceDate=&UpdatedSinceDate=&Pollutant=PM10&Namespace=&Format=XML&UserToken="  //i replaced the fixed date with the variable i 
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    
    print the_page
    


Answer: 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](https://docs.python.org/2/library/string.html#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. 

