Python socket 簡單示例程式

2021-05-23 20:36:34 字數 1400 閱讀 4277

pyhton 的socket 通訊簡單方便,寫了乙個小例子,真的挺方便的

客戶端:

import thread

from socket import *

host = '192.168.7.118'

port = 8888

bufsize = 20000

addr = (host,port)

clisock = socket(af_inet,sock_stream)

clisock.connect(addr)

print 'enter out'

def priword():

while true:

data = clisock.recv(bufsize)

if data:

print ':',data,'/n>',

thread.start_new_thread(priword,())

while true:

data = 'michael:'+raw_input('-->')

if not data:

break

clisock.send(data)

clisock.close()

服務端:

import thread

from socket import *

from time import ctime

host = ''

port = 8888

bufsize = 200000

add = (host,port)

sersock = socket(af_inet,sock_stream)

sersock.bind(add)

sersock.listen(10)

while true:

print 'waiting for connecting...'

clisock,addr = sersock.accept()

print 'connected from:',addr

def priword():

while true:

data = clisock.recv(bufsize)

if data:

print data,'/n>'

thread.start_new_thread(priword,())

while true:

reword = "bill:"+raw_input('-->')

if not reword:

break

clisock.send(reword)

clisock.close()

if not raw_input('/nenter out :'):

break

sersock.close()

python socket 簡單程式設計

伺服器端 import socket s socket.socket host socket.gethostname port 1234 s.bind host,port s.listen 5 while true c,addr s.accept print got connection from ...

NSDictionary plist簡單示例

在iphone ipad工程裡面,新增 file other property list,例如 test.plist,然後在其中新增3個專案 key name,date,dept,並填充value值。以下為對此test.plist檔案的一系列常用操作 myname,mydate,mydept為定義的...

NSDictionary plist簡單示例

在iphone ipad工程裡面,新增 file other property list,例如 test.plist,然後在其中新增3個專案 key name,date,dept,並填充value值。以下為對此test.plist檔案的一系列常用操作 myname,mydate,mydept為定義的...