How to download a file using Python

To download a file using Python you can use the snippet below.

Sample Python

import urllib.request
import shutil

# file_name = target file name
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)

Please rate this snippet

How to use the HTML5 datalist tag

HTML5 Datalist Element

Description

HTML5 introduced the new <datalist> tag, which specifies a list of predefined <input> element options. It is used to provide the autocomplete feature on <input> elements. Users will see a drop-down list of pre-defined options as they input data. This element has no other attributes than the global attributes, common to all elements. The id of the datalist element must match with the list attribute of the input box.Keep in mind that Safari doesn’t support datalist yet. If you are using this element with an unsupported (older) browser version, the most browsers will simply ignores the datalist tag and render rest of the webpage. Internet Explorer 9 and below is a little bit special, it will simply print the option values to the page (a possible workaround can be found at http://viralpatel.net/).

Usage

Here you can see a Basic example on how to use the datalist tag in HTML5.

Live Sample

Keep in mind that this Live-Example only works on supported Browsers.



JSFiddle’s

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Compatibility:
working Chrome
working Internet Explorer
not tested Firefox
not working Opera
not working Safari

Please rate this snippet

How to check if an array contains a value in Javascript

To check if an array contains a value in Javascript you can use the snippet below.

Sample Javascript

Array.prototype.inArray = function (inputValue) {
	var counter;
	for (counter=0; counter < this.length; counter++) {
		if (this[counter] === inputValue) {
			return true;
		}
	}
	return false;
};

Please rate this snippet

How to remove any post that is older than X days in WordPress

To remove any post that is older than X days in WordPress you can use the query below.

Sample mySQL

DELETE FROM wp_posts WHERE post_type = 'post' AND DATEDIFF(NOW(), post_date) > X

Please rate this snippet

How to disable or enable Trackbacks and Pingbacks in WordPress

To disable or enable Trackbacks and Pingbacks in WordPress you can use the following mysql queries.

Sample mySQL

enable

UPDATE wp_posts SET ping_status = 'open'; 

disable

UPDATE wp_posts SET ping_status = 'closed'; 

Please rate this snippet

How to set the HTTP Header for a request using AngularJs

To set the HTTP Header for a request using AngularJs you can use the following snippet.

$http({method: 'GET', url: 'www.fesslersoftexample.com/api', headers: {
    'Authorization': 'Basic YmVlcDpib29w'}
});

Use the headers parameter in the config object you pass to $http.

For more informations see the AngularJs API Reference

Please rate this snippet

How to send Data using a WebRequest in C# and VB.NET

To send/post Data using a WebRequest in C# and VB.NET you can use the following snippet.

Sample C#

public static string WebrequestWithPost(string url, Encoding dataEncoding, string dataToPost, string contentType = @"application/x-www-form-urlencoded")
{
	var postDataAsByteArray = dataEncoding.GetBytes(dataToPost);
	var returnValue = String.Empty;
	try
	{
		var webRequest = WebRequest.CreateHttp(url);  //change to: var webRequest = (HttpWebRequest)WebRequest.Create(url); if you are your .NET Version is lower than 4.5
		if (webRequest != null)
		{
			webRequest.AllowAutoRedirect = false;
			webRequest.Method = "POST";
			webRequest.ContentType = contentType;
			webRequest.ContentLength = postDataAsByteArray.Length;
			using (var requestDataStream = webRequest.GetRequestStream())
			{
				requestDataStream.Write(postDataAsByteArray, 0, postDataAsByteArray.Length);
				requestDataStream.Close();
				using (var response = webRequest.GetResponse())
				{
					using (var responseDataStream = response.GetResponseStream())
					{
						if (responseDataStream != null)
						{
							using (var responseDataStreamReader = new StreamReader(responseDataStream))
							{
								returnValue = responseDataStreamReader.ReadToEnd();
								responseDataStreamReader.Close();
							}
							responseDataStream.Close();
						}
					}
					response.Close();
				}
				requestDataStream.Close();
			}
		}
	}
	catch (WebException ex)
	{
		if (ex.Status == WebExceptionStatus.ProtocolError)
		{
			var response = ((HttpWebResponse)ex.Response);
			//handle this your own way.
			Console.WriteLine("Webexception! Statuscode: {0}, Description: {1}",(int)response.StatusCode,response.StatusDescription);
		}
	}
	catch(Exception ex) 
	{
		//handle this your own way, something serious happened here.
		Console.WriteLine(ex.Message);
	}
	return returnValue;
}

Sample VB.NET

Public Shared Function WebrequestWithPost(ByVal url As String, ByVal dataEncoding As Encoding, ByVal dataToPost As String, ByVal contentType As String) As String
    Dim postDataAsByteArray As Byte() = dataEncoding.GetBytes(dataToPost)
    Dim returnValue As String = String.Empty
    Try
        Dim webRequest As HttpWebRequest = WebRequest.CreateHttp(url)  'change to: dim webRequest as var = DirectCast(WebRequest.Create(url), HttpWebRequest) if you are your .NET Version is lower than 4.5
        If (Not (webRequest) Is Nothing) Then
            webRequest.AllowAutoRedirect = false
            webRequest.Method = "POST"
            webRequest.ContentType = contentType
            webRequest.ContentLength = postDataAsByteArray.Length
            Dim requestDataStream As Stream = webRequest.GetRequestStream
            requestDataStream.Write(postDataAsByteArray, 0, postDataAsByteArray.Length)
            requestDataStream.Close
            Dim response As Webresponse = webRequest.GetResponse
            Dim responseDataStream As Stream = response.GetResponseStream
            If (Not (responseDataStream) Is Nothing) Then
                Dim responseDataStreamReader As StreamReader = New StreamReader(responseDataStream)
                returnValue = responseDataStreamReader.ReadToEnd
                responseDataStreamReader.Close
                responseDataStream.Close
            End If
            response.Close
            requestDataStream.Close
        End If
    Catch ex As WebException
        If (ex.Status = WebExceptionStatus.ProtocolError) Then
            Dim response As HttpWebResponse = CType(ex.Response,HttpWebResponse)
            'handle this your own way.
            Console.WriteLine("Webexception! Statuscode: {0}, Description: {1}", CType(response.StatusCode,Integer), response.StatusDescription)
        End If
    Catch ex As Exception
        'handle this your own way, something serious happened here.
        Console.WriteLine(ex.Message)
    End Try
    Return returnValue
End Function

FOR MORE INFORMATIONS SEE THE MSDN: How to: Send Data Using the WebRequest Class

Please rate this snippet