Python Tutorial ( PDFDrive )
Next, call the accept method of the returned object. This method waits until a client connects to the port youspecified and then returns a connection object that represents the connection to that client.#!/usr/bin/pythonimport socket# This is server.py file# Import socket modules = socket.socket()# Create a socket objecthost = socket.gethostname() # Get local machine nameport = 12345# Reserve a port for your service.s.bind((host, port)) # Bind to the ports.listen(5)# Now wait for client connection.while True:c, addr = s.accept() # Establish connection with client.print 'Got connection from', addrc.send('Thank you for connecting')c.close()# Close the connectionA Simple Client: Now, we will write a very simple client program, which will open a connection to a given port 12345 and givenhost. This is very simple to create a socket client using Python's socket module function.The socket.connect(hosname, port ) opens a TCP connection to hostname on the port. Once you have asocket open, you can read from it like any IO object. When done, remember to close it, as you would close a file.The following code is a very simple client that connects to a given host and port, reads any available data fromthe socket, and then exits:#!/usr/bin/pythonimport socket# This is client.py file# Import socket modules = socket.socket()# Create a socket objecthost = socket.gethostname() # Get local machine nameport = 12345# Reserve a port for your service.s.connect((host, port))print s.recv(1024)s.close# Close the socket when doneNow, run this server.py in background and then run above client.py to see the result.# Following would start a server in background.$ python server.py &# Once server is started run client as follows:$ python client.pyThis would produce the following result:Got connection from ('127.0.0.1', 48437)Thank you for connectingPython Internet modules A list of some important modules, which could be used in Python Network/Internet programming.TUTORIALS POINT Simply Easy Learning
Protocol Common function Port No Python moduleHTTP Web pages 80 httplib, urllib, xmlrpclibNNTP Usenet news 119 nntplibFTP File transfers 20 ftplib, urllibSMTP Sending email 25 smtplibPOP3 Fetching email 110 poplibIMAP4 Fetching email 143 imaplibTelnet Command lines 23 telnetlibGopher Document transfers 70 gopherlib, urllibPlease check all the libraries mentioned above to work with FTP, SMTP, POP, and IMAP protocols.Further Readings: I have given you a quick start with Socket Programming. It's a big subject, so its recommended togo through the following link to find more details on:• Unix Socket Programming.• Python Socket Library and Modules.TUTORIALS POINT Simply Easy Learning
- 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 and 348: ProgrammingErrorSubclass of Databas
- Page 349: The socket Module: To create a so
- 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
- Page 399 and 400: Parameters: • master: This repres
Protocol Common function Port No Python module
HTTP Web pages 80 httplib, urllib, xmlrpclib
NNTP Usenet news 119 nntplib
FTP File transfers 20 ftplib, urllib
SMTP Sending email 25 smtplib
POP3 Fetching email 110 poplib
IMAP4 Fetching email 143 imaplib
Telnet Command lines 23 telnetlib
Gopher Document transfers 70 gopherlib, urllib
Please check all the libraries mentioned above to work with FTP, SMTP, POP, and IMAP protocols.
Further Readings:
I have given you a quick start with Socket Programming. It's a big subject, so its recommended to
go through the following link to find more details on:
• Unix Socket Programming.
• Python Socket Library and Modules.
TUTORIALS POINT
Simply Easy Learning