Poll

Do you want an Tutorial about FTP,SMTP,IMAP and POP in Python too?

Yes,please !
No,thanks...

Author Topic: [Tutorial@Python] Network communication  (Read 6295 times)

0 Members and 1 Guest are viewing this topic.

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [Tutorial@Python] Network communication
« Reply #15 on: January 31, 2012, 07:49:32 pm »
Just suggesting things to write about :) http://ilab.cs.byu.edu/python/threadingmodule.html
This is just a chat application like the upper ones.Does not really more,just looks a bit more complex and is created as a class.
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [Tutorial@Python] Network communication
« Reply #16 on: February 26, 2012, 06:12:13 pm »
1.2.0 FTP

With the ftplib module you can connect to a ftp-server. FTP stands for file transfer protocol and is for transfering files in TCP/IP networks. You can identify a ftp-server at the ftp:// at the beginning of the url-bar.

The FTP protocol works on two channels: one for sending the commands to the server,and one for sending files. Because of the two channel
[/b]technique you can e.g. stop a download while downloading a file.

Let's start with connecting to a ftp-server:
Code: (python) [Select]
import ftplib
ftp = ftplib.FTP('ftp://ftp.example.com')

First we import the mdule with 'import',then we create a new ftp instance with 'ftplib.FTP' and connect with the given server 'ftp.example.com'

Then we will have to login on most servers:
Code: (python) [Select]
ftp.login('myusername','mypass')
If you want to see the welcome message you can use 'ftp.getwelcome()'

To stop a download use 'ftp.abort()'

To send a command to the ftp-server use:
Code: (python) [Select]
ftp.sendcmd('command')
To download in binary mode use:
Code: (python) [Select]
ftp.retrbinary('RETR file',function)
An example for downloading a picture:
Code: (python) [Select]
def f(data):
    file = ''
    file += data
    return file
pic = ftp.retrbinary('RETR picture.jpg',f)

To download in ASCII mode use:
Code: (python) [Select]
ftp.retrlines('RETR file',function)
To upload in binary mode use:
Code: (python) [Select]
ftp.storbinary('STOR file',function)
The same for ASCII mode upload:
Code: (python) [Select]
ftp.storlines('STOR file',f)
To see the content of the actual directory as as  a list use 'ftp.nlst()'
to see the content as a listing use'ftp.dir()'

To rename a file use:
Code: (python) [Select]
ftp.rename('oldname','newname')
To delete use:
Code: (python) [Select]
ftp.delete('file')
To change working directory use:
Code: (python) [Select]
ftp.cwd('directory')
To create a new one use:
Code: (python) [Select]
ftp.mkd('directory')
To get the working directory use:
Code: (python) [Select]
ftp.pwd()
You can rename a folder with:
Code: (python) [Select]
ftp.rnd('folder')
To close the connection use:
Code: (python) [Select]
ftp.quit()
If you can't close the connection with 'ftp.quit()' use 'ftp.close()' to kill the connection without informing the server. You can't login to the ftp-server after useing 'ftp.close()' without recreating the ftp instance and reconnecting to the server.
« Last Edit: May 13, 2012, 09:24:05 pm by Area_13 »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [Tutorial@Python] Network communication
« Reply #17 on: February 26, 2012, 06:32:00 pm »
1.3.0 Email

You can send emails useing the SMTP protocol (SMTP stands for Simple Mail Transfer Protocol), to raed the recieved emails you can use the POP3 and IMAP4 protocols.

Let's start with the SMTP protocol:


1.3.1 SMTP

The SMTP protocol is used to send emails via a smtp-server. As the smtp protocol wasn't enough anymore the ESMTP protcol was created (Extended SMTP).

Let's start with connecting to a smtp-server:
Code: (python) [Select]
import smtplib
smtp = smtplib.SMTP('smtp.example.com')
First we import the smtp module,then we connect to a smtp-server (btw,the address doesn't always start with the protocol name,because sometimes the server has an other name (e.g. 'mail.example.com').

Like at FTP we also have to login to our email account:
Code: (python) [Select]
smtp.login('myusername','mypass')
Sending mails looks a bit more complex:
Code: (python) [Select]
smtp.sendmail('from','to','message'[,mailoptions,rctpoptions])
For example a simple email:
Code: (python) [Select]
smtp.sendmail('mymail@mail.com','friendsmail@othermail.com','Hello World')Or with more receiver:
Code: (python) [Select]
smtp.sendmail('mymail@mail.com',['firendsmail@othermail.com','fiend2smail@example.com'],'Hellow World')
To quit use:
Code: (python) [Select]
smtp.quit()
(I'll show you a very basic smtp-client after the email-module)



POP3 and IMAP4 will come soon...

« Last Edit: May 13, 2012, 09:24:37 pm by Area_13 »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline Ragehottie

  • Knight
  • **
  • Posts: 313
  • Cookies: -9
  • Hack to learn, not learn to hack.
    • View Profile
Re: [Tutorial@Python] Network communication
« Reply #18 on: June 03, 2012, 04:27:51 am »
I'm going to bump this because I use it alot and it was helpful to me as a Python newb.
Blog: rexmckinnon.tumblr.com