Created
August 15, 2012 15:20
-
-
Save VincentVetsch/3360982 to your computer and use it in GitHub Desktop.
Python: Lookup a website and make a connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# [SNIPPET_NAME: Lookup website] | |
# [SNIPPET_CATEGORIES: GIO] | |
# [SNIPPET_DESCRIPTION: Lookup a website and make a connection] | |
# [SNIPPET_AUTHOR: Andrew Breiner <[email protected]>] | |
# [SNIPPET_LICENSE: GPL] | |
# [SNIPPET_DOCS: http://www.pygtk.org/docs/pygobject/gio-class-reference.html] | |
# This example was taken from an example done in the Vala programming | |
# language. | |
import gio | |
# Setup a resolver and lookup the ip address for www.google.com | |
resolver = gio.resolver_get_default() | |
addresses = resolver.lookup_by_name("www.google.com") | |
# Print the ip addresses that are associated with www.google.com | |
print "www.google.com resolves to :" | |
for i in range(0, len(addresses)): | |
print addresses[i].to_string() | |
# Connect to www.google.com | |
client = gio.SocketClient() | |
socket = gio.InetSocketAddress(addresses[0], 80) | |
conn = client.connect(socket, gio.Cancellable()) | |
# Send a message to www.google.com | |
message = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"; | |
stream = conn.get_output_stream() | |
stream.write (message, gio.Cancellable()) | |
# Recieve a message from www.google.com which is | |
# HTTP/1.1 200 OK | |
istream = gio.DataInputStream (conn.get_input_stream()) | |
message = istream.read_line() | |
print "received status line: " + message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# [SNIPPET_NAME: Lookup website] | |
# [SNIPPET_CATEGORIES: GIO] | |
# [SNIPPET_DESCRIPTION: Lookup a website and make a connection] | |
# [SNIPPET_AUTHOR: Andrew Breiner <[email protected]>] | |
# [SNIPPET_LICENSE: GPL] | |
# [SNIPPET_DOCS: http://www.pygtk.org/docs/pygobject/gio-class-reference.html] | |
# This example was taken from an example done in the Vala programming | |
# language. | |
import gio | |
# Setup a resolver and lookup the ip address for www.google.com | |
resolver = gio.resolver_get_default() | |
addresses = resolver.lookup_by_name("www.google.com") | |
# Print the ip addresses that are associated with www.google.com | |
print "www.google.com resolves to :" | |
for i in range(0, len(addresses)): | |
print addresses[i].to_string() | |
# Connect to www.google.com | |
client = gio.SocketClient() | |
socket = gio.InetSocketAddress(addresses[0], 80) | |
conn = client.connect(socket, gio.Cancellable()) | |
# Send a message to www.google.com | |
message = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"; | |
stream = conn.get_output_stream() | |
stream.write (message, gio.Cancellable()) | |
# Recieve a message from www.google.com which is | |
# HTTP/1.1 200 OK | |
istream = gio.DataInputStream (conn.get_input_stream()) | |
message = istream.read_line() | |
print "received status line: " + message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment