python socket簡單網絡編程

  1. server端:

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', addr
    print c.recv(1024)
    c.send('Thank you for your connection')
    c.close()

2. 客戶端

import socket

s = socket.socket()

host = socket.gethostname()
port = 1234

s.connect((host, port))
s.send('nihao')
print s.recv(1024)

同時在一臺計算機上運行,可以觀察到效果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章