用Python套接字创建HTTP客户与服务器程序

最近在学习python,网络编程中,python寥寥几句,就可以创建一个服务端和客户端程序:

服务端:

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
    c.send('Thank you for connecting')
    c.close

客户端:

import socket

s = socket.socket()

host = socket.gethostname()
port = 1234

s.connect((host, port))
print s.recv(1024)

测试如图:

是不是瞬间感觉到了python的强大!哈哈,再加上python强大的字符处理能力,就难怪python要火了!

原文地址:https://www.cnblogs.com/yuanzhaoyi/p/5950341.html