Python Tutorial ( PDFDrive )
CHAPTER22Python NetworkingPython provides two levels of access to network services. At a low level, you can access the basic socketsupport in the underlying operating system which allows you to implement clients and servers for both connectionorientedand connectionless protocols.Python also has libraries that provide higher-level access to specific application-level network protocols, such asFTP, HTTP, and so on.This tutorial gives you understanding on most famous concept in Networking - Socket ProgrammingWhat are Sockets? Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within aprocess, between processes on the same machine, or between processes on different continents.Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and soon. The socket library provides specific classes for handling the common transports as well as a generic interfacefor handling the rest.Sockets have their own vocabulary:TermdomaintypeprotocolhostnameportDescriptionThe family of protocols that will be used as the transport mechanism. These values areconstants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.The type of communications between the two endpoints, typically SOCK_STREAM forconnection-oriented protocols and SOCK_DGRAM for connectionless protocols.Typically zero, this may be used to identify a variant of a protocol within a domain and type.The identifier of a network interface:• A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon(and possibly dot) notation• A string "<broadcast>", which specifies an INADDR_BROADCAST address.• A zero-length string, which specifies INADDR_ANY, or• An Integer, interpreted as a binary address in host byte order.Each server listens for clients calling on one or more ports. A port may be a Fixnum portnumber, a string containing a port number, or the name of a service.TUTORIALS POINT Simply Easy Learning
The socket Module: To create a socket, you must use the socket.socket() function available in socket module, which has the generalsyntax:s = socket.socket (socket_family, socket_type, protocol=0)Here is the description of the parameters:• socket_family: This is either AF_UNIX or AF_INET, as explained earlier.• socket_type: This is either SOCK_STREAM or SOCK_DGRAM.• protocol: This is usually left out, defaulting to 0.Once you have socket object, then you can use required functions to create your client or server program.Following is the list of functions required:Server Socket Methods: Methods.bind()s.listen()s.accept()DescriptionThis method binds address (hostname, port number pair) to socket.This method sets up and start TCP listener.This passively accepts TCP client connection, waiting until connection arrives (blocking).Client Socket Methods: Methods.connect()DescriptionThis method actively initiates TCP server connection.General Socket Methods: Methods.recv()s.send()s.recvfrom()s.sendto()s.close()socket.gethostname()DescriptionThis method receives TCP messageThis method transmits TCP messageThis method receives UDP messageThis method transmits UDP messageThis method closes socketReturns the hostname.A Simple Server: To write Internet servers, we use the socket function available in socket module to create a socket object. Asocket object is then used to call other functions to set up a socket server.Now, call bind(hostname, port function to specify a port for your service on the given host.TUTORIALS POINT Simply Easy Learning
- Page 297 and 298: Description The method ttyname() re
- Page 299 and 300: Return Value This method does not
- Page 301 and 302: Return Value This method returns t
- Page 303 and 304: KeyErrorNameErrorUnboundLocalErrorE
- Page 305 and 306: ......................else:If there
- Page 307 and 308: print "Error: can\'t find file or r
- Page 309 and 310: self.args = argSo once you defined
- Page 311 and 312: 'Optional class documentation strin
- Page 313 and 314: For the above class let's try to ac
- Page 315 and 316: print 'Calling child method'c = Chi
- Page 317 and 318: Data Hiding: An object's attribute
- Page 319 and 320: groups()This method returns all mat
- Page 321 and 322: SYNTAX: re.sub(pattern, repl, strin
- Page 323 and 324: \s Matches whitespace. Equivalent t
- Page 325 and 326: Backreferences: This matches a prev
- Page 327 and 328: CHAPTER20Python CGI ProgrammingWhat
- Page 329 and 330: If you click hello.py, then this pr
- Page 331 and 332: Simple URL Example : Get Metho
- Page 333 and 334: </form>The result of this code is t
- Page 335 and 336: text_content = form.getvalue('textc
- Page 337 and 338: Here is an example of how to retrie
- Page 339 and 340: CHAPTER21Python Database AccessThe
- Page 341 and 342: EXAMPLE: Following is the example o
- Page 343 and 344: ('Mac', 'Mohan', 20, 'M', 2000)try:
- Page 345 and 346: DELETE Operation: DELETE operation
- Page 347: ProgrammingErrorSubclass of Databas
- Page 351 and 352: Protocol Common function Port No Py
- Page 353 and 354: Subject: SMTP e-mail testThis is a
- Page 355 and 356: try:smtpObj = smtplib.SMTP('localho
- Page 357 and 358: count = 0while count < 5:time.sleep
- Page 359 and 360: Synchronizing Threads: The threadi
- Page 361 and 362: thread = myThread(threadID, tName,
- Page 363 and 364: <stars>10</stars><description>Talk
- Page 365 and 366: print "Rating:", self.ratingelif se
- Page 367 and 368: Format: DVDRating: PGDescription: T
- Page 369 and 370: Tkinter Widgets Tkinter provides v
- Page 371 and 372: fgfontheighthighlightcolorimagejust
- Page 373 and 374: WidthXscrollincrementXscrollcommand
- Page 375 and 376: pattern when it is over the checkbu
- Page 377 and 378: The Entry widget is used to accept
- Page 379 and 380: E1 = Entry(top, bd =5)E1.pack(side
- Page 381 and 382: Optionanchorbgbitmapbdcursorfontfgh
- Page 383 and 384: selectmodeDetermines how many items
- Page 385 and 386: Syntax: Here is the simple syntax t
- Page 387 and 388: Menu The goal of this widget is to
- Page 389 and 390: filemenu.add_command(label="Exit",
- Page 391 and 392: Example: Try the following example
- Page 393 and 394: WidthWraplengthWidth of the label i
- Page 395 and 396: label appears in the top left corne
- Page 397 and 398: Parameters: • master: This repres
The socket Module:
To create a socket, you must use the socket.socket() function available in socket module, which has the general
syntax:
s = socket.socket (socket_family, socket_type, protocol=0)
Here is the description of the parameters:
• socket_family: This is either AF_UNIX or AF_INET, as explained earlier.
• socket_type: This is either SOCK_STREAM or SOCK_DGRAM.
• protocol: This is usually left out, defaulting to 0.
Once you have socket object, then you can use required functions to create your client or server program.
Following is the list of functions required:
Server Socket Methods:
Method
s.bind()
s.listen()
s.accept()
Description
This method binds address (hostname, port number pair) to socket.
This method sets up and start TCP listener.
This passively accepts TCP client connection, waiting until connection arrives (blocking).
Client Socket Methods:
Method
s.connect()
Description
This method actively initiates TCP server connection.
General Socket Methods:
Method
s.recv()
s.send()
s.recvfrom()
s.sendto()
s.close()
socket.gethostname()
Description
This method receives TCP message
This method transmits TCP message
This method receives UDP message
This method transmits UDP message
This method closes socket
Returns the hostname.
A Simple Server:
To write Internet servers, we use the socket function available in socket module to create a socket object. A
socket object is then used to call other functions to set up a socket server.
Now, call bind(hostname, port function to specify a port for your service on the given host.
TUTORIALS POINT
Simply Easy Learning