Python 學習積累《一》

2021-05-27 15:54:11 字數 4102 閱讀 9812

1. python 學習**

(老王python)

2. 通過commandline 執行 .py 指令碼

首先要將python的安裝目錄加入到path環境變數中。如:

path=%path%;c:\python27再到你儲存py原始檔的目錄下執行:

《指令碼路徑》   python  

helloworld.py

3. python 通過命令列傳引數

例項:

import sys

if(len(sys.argv)>2):

print "aaaa"

sys.exit(0)

print "commandline parameter 1: ",sys.argv[1]

print "commandline parameter 2: ",sys.argv[2]

4. python 實現socket通訊(tcp)

例項:(在2.7上測試通過)

server 端**:

import socket

address1=('127.0.0.1',2011)

s=socket.socket(socket.af_inet, socket.sock_stream)

s.bind(address1)

s.listen(5)

cs,address = s.accept()

print 'got connected from',address

cs.send('hello i am server,welcome')

while 1:

ra=cs.recv(512)

print ra

client端**:

import socket

address=('127.0.0.1',2011)

s=socket.socket()

s.connect(address)

data=s.recv(512)

print 'the data received is/n ',data

s.send('hihi i am client')

while 1:

sinput=raw_input("enter message and send to server:\n")

s.send('from client: %s'%sinput)

5. python 發郵件**:

import os   

import smtplib

import mimetypes

from email.mimemultipart import mimemultipart

from email.mimebase import mimebase

from email.mimetext import mimetext

from email.mimeaudio import mimeaudio

from email.mimeimage import mimeimage

from email.encoders import encode_base64

def sendmail(subject, text, *attachmentfilepaths):

gmailuser = '[email protected]'

gmailpassword = '******'

recipient = '[email protected]'

msg = mimemultipart()

msg['from'] = gmailuser

msg['to'] = recipient

msg['subject'] = subject

msg.attach(mimetext(text))

for attachmentfilepath in attachmentfilepaths:

msg.attach(getattachment(attachmentfilepath))

mailserver = smtplib.smtp('smtp.163.com', 25)

mailserver.ehlo()

mailserver.starttls()

mailserver.ehlo()

mailserver.login(gmailuser, gmailpassword)

mailserver.sendmail(gmailuser, recipient, msg.as_string())

mailserver.close()

print('sent email to %s' % recipient)

def getattachment(attachmentfilepath):

contenttype, encoding = mimetypes.guess_type(attachmentfilepath)

if contenttype is none or encoding is not none:

maintype, subtype = contenttype.split('/', 1)

file = open(attachmentfilepath, 'rb')

if maintype == 'text':

attachment = mimetext(file.read())

elif maintype == 'message':

attachment = email.message_from_file(file)

elif maintype == 'image':

attachment = mimeimage(file.read(),_subtype=subtype)

elif maintype == 'audio':

attachment = mimeaudio(file.read(),_subtype=subtype)

else:

attachment = mimebase(maintype, subtype)

attachment.set_payload(file.read())

encode_base64(attachment)

file.close()

attachment.add_header('content-disposition', 'attachment', filename=os.path.basename(attachmentfilepath))

return attachment

# start to test

sendmail('hi,cheers li', 'greetings from lihuichang')

6. python 獲取本機計算機名和ip

方法一:

import socket

name=socket.gethostname()

print name

ip_addr=socket.gethostbyname(name)

print ip_addr

方法二:

from socket import socket, sock_dgram, af_inet  

s = socket(af_inet, sock_dgram)

s.connect(('google.com', 0))

print s.getsockname()

7. pyhton 網路程式設計之多執行緒

Linux Shell 學習積累一

1.linux 下如何寫shell 指令碼 2.shell指令碼自動輸入y yes 實現簡單的互動 shell指令碼自動輸入y yes實現簡單的互動 例如 執行指令碼時迴圈出現 y n 讓你選擇,怎樣寫乙個指令碼使其自動選擇y。非常簡單 yes 你的指令碼 例如 yes test.sh 注意 不是 ...

vim 學習積累(一)

首先是簡單的認識了三種狀態 大家公認的說法是模式 分別是 插入,檢視,和一般。進入vim之後預設的是一般模式,這時直接使用 a i o 也就是進入vim之後直接按下a i o鍵均可進入插入模式。其中的區別是,a命令會另游標後退乙個字元,然後進入插入模式。i是直接進入插入模式,游標位置不變。o會另游標...

python 語言學習積累

1 python中的argparse模組使用 argparse是python用於解析命令列引數和選項的標準模組 argparse 使用 使用argparse 配置命令列引數時,需要三步 建立 argumentparser 物件 呼叫 add argument 方法新增引數 使用 parse args...