Download Domain Name System (DNS)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Remote Desktop Services wikipedia , lookup

IEEE 1355 wikipedia , lookup

Zero-configuration networking wikipedia , lookup

Lag wikipedia , lookup

Real-Time Messaging Protocol wikipedia , lookup

Streaming media wikipedia , lookup

Transcript
Domain Name System (DNS)
is a distributed database that's typically used
to resolve hostnames into IP addresses.
There are several different DNS modules
available for Python: PyDNS, dnspython,
adns...
DNS is a massive, globally distributed
database. It provides a series of referrals,
each giving a more specific answer, until the
final answer is obtained.

Operating System Lookup Services
The operating system comes with several
functions for DNS lookups (often called
resolver libraries), which provide
everything needed for most applications.
 Many UNIX systems contain a file called
/etc/hosts that defines hostnames and IP
addresses.

Fetching Web Pages
# Obtain Web Page
import sys, urllib2
req = urllib2.Request(sys.argv[1])
fd = urllib2.urlopen(req)
while 1:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)
urllib2.urlopen()
# Obtain Web Page Information
import sys, urllib2
req = urllib2.Request(sys.argv[1])
fd = urllib2.urlopen(req)
print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
print "%s = %s" % (key, value)
dump_page.py
ftp://ftp.ibiblio.org/README


The urllib2 module does support non-HTTP
protocols. By default, it will support HTTP, files on
your machine's local hard drive, and FTP, but you
can also enable Gopher handler.
The only difference that's noticeable to your
program will be the headers returned by info(). In
some cases, there may be no headers at all. In
others, HTTP headers may be emulated.
FTP - the File Transfer Protocol
# ASCII download
# Downloads README from remote and writes it to disk.
from ftplib import FTP
def writeline(data):
fd.write(data + "\n")
f = FTP('ftp.kernel.org')
f.login()
f.cwd('/pub/linux/kernel')
fd = open('README', 'wt')
f.retrlines('RETR README', writeline)
fd.close()
f.quit()
HTMLParser
HTMLParser is one of several choices you
have for parsing HTML. Python's standard
library ships htmllib, which is based on
Python's more general SGML Framework.
 To implement a parser with the
HTMLParser module, you'll generally
subclass HTMLParser.HTMLParser and
add functions to handle different types of
tags.

To pull the title out of the HTML doc.
from HTMLParser import HTMLParser
import sys
class TitleParser(HTMLParser):
def __init__(self):
self.title = ''
self.readingtitle = 0
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag == 'title':
self.readingtitle = 1
def handle_data(self, data):
if self.readingtitle:
# Ordinarily, this is slow and a bad practice, but
# we can get away with it because a title is usually
# small and simple.
self.title += data
def handle_endtag(self, tag):
if tag == 'title':
self.readingtitle = 0
def gettitle(self):
return self.title
<TITLE>Document Title</TITLE>
fd = open(sys.argv[1])
tp = TitleParser()
tp.feed(fd.read())
print "Title is:", tp.gettitle()
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
This is my text.
</BODY>
</HTML>
Title is: Document Title
port assignments for common protocols
TCP protocol with a client and server written
using the socket module
For TCP servers, the socket object used to receive connections is not
the same socket used to perform subsequent communication with the
client. In particular, the accept() system call returns a new socket object
that’s actually used for the connection. This allows a server to manage
connections from a large number of clients simultaneously.
The client program:
UDP communication
UDP communication is performed
in a similar manner as TCP
communicaion except that clients
and servers don’t establish a
“connection” with each other
getaddrinfo()
# Basic getaddrinfo() - basic example
import sys, socket
result = socket.getaddrinfo(sys.argv[1], None)
print result[0][4]
gethostbyaddr
import sys, socket
try:
# Perform the lokoup
result = socket.gethostbyaddr(sys.argv[1])
# Display the looked-up hostname
print "Primary hostname:"
print " " + result[0]
# Display the list of available addresses
print "\nAddresses:"
for item in result[2]:
print " " + item
except socket.herror, e:
print "Couldn't look up name:", e
Receiving Streaming Data Using the
In addition to the socket
ServerSocket Module
To handle the streaming requests, override the
handle method to read and process the streaming
data. The rfile.readline() function reads the
streaming data until a newline character is
encountered, and then returns the data as a string.
To send data back to the client from the streaming
server, use the wfile.write(string) command to write
the string back to the client.
module, Python includes the
SocketServer module to
provide you with TCP, UDP,
and UNIX classes that
implement servers. These
classes have methods that
provide you with a much higher
level of socket control.
To implement a SocketServer
to handle streaming requests,
first define the class to inherit
from the
SocketServer.StreamRequest
Handler class.
After the server object has been created, you can start handling connections by
invoking the server object's handle_request() or serve_forever() method.
Sending Streaming Data
To send streaming data to the
streaming server described in
the previous task, first create
the client socket by calling
socket(family, type [, proto]),
which creates and returns a
new socket.
Once the streaming client-side
socket has been created, it
can connect to the streaming
server using the
connect(address) method,
where address refers to a
tuple in the form of
(hostname, port).
After the streaming client-side socket has connected to the server-side
socket, data can be streamed to the server by formatting a stream of data
that ends with the newline character and sending it to the server using
the send(string [,flags]) method.
A response from the server is received from the socket using the
recv(buffsize [,flags]) method.
Sending Email Using SMTP
• The smtplib module included with Python provides simple access to SMTP
servers that allow you to connect and quickly send mail messages from your
Python scripts.
• Mail messages must be formatted properly for the To, From, Date, Subject, and
text fields to be processed properly by the SMTP mail server. The code in
send_smtp.py shows the proper formatting for the mail message, including the
item headers and newline characters.
• Once the mail message is properly formatted, connect to the SMTP server using
the smtplib.SMTP(host [,port]) method.