Python乙個簡單的通訊程式 客戶端 伺服器

2021-06-21 05:25:07 字數 1988 閱讀 5464

功能是從客戶端向服務傳送乙個字串, 伺服器收到後將字串重新傳送給客戶端,同時,在連線建立之後,伺服器可以向客戶端傳送任意多的字串

客戶端:

10.248.27.23是我電腦的ip

import socket, sys

host = '10.248.27.23'

# host = raw_input("plz imput destination ip:")

# data = raw_input("plz imput what you want to submit:")

port = 51423

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

try:

s.connect((host, port))

except socket.gaierror, e:

print "address-related error connecting to server: %s" %e

sys.exit(1)

except socket.error, e:

print "connection error: %s" %e

sys.exit(1)

data = raw_input("plz imput what you want to submit:")

s.send(data)

s.shutdown(1)

print "submit complete"

while 1:

buf = s.recv(1024)

sys.stdout.write(buf)

伺服器:

import socket, traceback

host = ''

port = 51423

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

s.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1)

s.bind((host, port))

s.listen(1)

print "done"

while 1:

try:

clientsock, clientaddr = s.accept()

except keyboardinterrupt:

raise

except:

traceback.print_exc()

continue

#get informaion form client and reply

try:

print "get connect from ", clientsock.getpeername()

data = clientsock.recv(1024)

print "the information we get is %s" % str(data)

clientsock.sendall("i`ve got the information: ")

clientsock.sendall(data)

while 1:

str = raw_input("what you want to say:")

clientsock.sendall(str)

clientsock.sendall('\n')

except (keyboardinterrupt ,systemerror):

raise

except:

traceback.print_exc()

#clocs socket

try:

clientsock.close()

except keyboardinterrupt:

raise

except:

traceback.print_exc()

乙個簡單的串列埠通訊程式

從2015年到現在,將近4年沒有寫程式了,這次是乙個朋友要我做物聯網的專案,要學習一些新東西,做起來再說。主要 private void sp datareceived object sender,serialdatareceivedeventargs e else textbox2.text re...

乙個簡單的BIO通訊程式

同步阻塞io 簡稱bio 是最傳統的一種io模型,即在讀和寫的過程中會發生阻塞現象。我們編寫乙個簡單的服務端和客戶端程式,尋找一下同步阻塞i o的弊端 timeserver public class timeserver catch exception e finally 服務端建立乙個server...

乙個簡單的python程式

解答 首先可以從題目中看出除了1之外所有的奇數都是負數,所有的偶數都是正數,具體 如下 方法一 usr bin env python coding utf 8 author lisa li 求1 2 3 4 5 6.99的和 count 1 設定初始值 s1 0 接收計算所有偶數的變數 s2 0 接...