今天晚上學習了一下python的網路程式設計,實現了client向server傳送資料,server反饋資訊
python 3.3 版本
server :
from socket import *
class tcpserver(object):
def __init__ (self, serverport):
self.serversocket = socket(af_inet, sock_stream)
self.serversocket.bind(('', int(serverport)))
self.serversocket.listen(5)
def run(self):
print('the server socket is ready...')
while true:
connectionsocket, addr = self.serversocket.accept()
try:
message = connectionsocket.recv(4096)
print('received messages:',message)
connectionsocket.send(message)
except ioerror:
connectionsocket.send('404 not found')
connectionsocket.close()
connectionsocket.close()
self.serversocket.close()
if __name__ == '__main__':
server = tcpserver('20000')
server.run()
client:
import sys
import socket
import string
class tcpclient(object):
def __init__ (self, serverhost, serverport):
self.serverhost = serverhost
self.serverport = serverport
print(self.serverhost)
print(self.serverport)
while true:
clientsocket = socket.socket(socket.af_inet, socket.sock_stream)
clientsocket.connect((self.serverhost, int(self.serverport)))
print('connected to the server...')
str=input('please input string:')
clientsocket.send(str.encode())
responce = clientsocket.recv(4096)
print('received:', responce)
clientsocket.close()
if __name__ == "__main__":
client = tcpclient('10.22.73.16', '20000')
上面的ip和埠自己設定就可以了,本來我是想把client傳送訊息部分的**放在run函式裡的,但是編譯始終報錯,無語了,那就將就用吧。測試通過可以執行。 python網路程式設計 TCP網路程式設計
tcp程式設計 客戶端 import socket 1 套接字 tcp socket socket.socket socket.af inet,socket.sock stream 2 建立鏈結 tcp socket.connect 172.27.35.1 8080 3 傳送資訊 tcp socke...
python網路程式設計
網路通訊是計算機之間的程序之間的通訊。tcp程式設計 tcp連線建立是雙向通道,客戶端與服務端都可以給對方傳送資料。建立tcp連線時,主動發起連線的叫客戶端,被動響應連線的叫服務端。建立乙個tcp的socket連線 用socket family,type 建立套接字獲得socket物件。family...
Python 網路程式設計
usr bin env python encoding utf 8 import socket 通過socket模仿http,獲取baidu網頁 s socket.socket socket.af inet,socket.sock stream af inet指定ipv4協議,sock stream...