Beginning Python From Novice to Professional (9) - Socket

Socket

小型服务器:

#!/usr/bin/env 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()

小型客户机:

#!/usr/bin/env python
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.connect((host,port))
print s.recv(1024)

运行服务器后运行客户机程序:

服务器打印:

Got connection from ('127.0.1.1', 61625)
Got connection from ('127.0.1.1', 61626)
Got connection from ('127.0.1.1', 61627)
Got connection from ('127.0.1.1', 61628)
Got connection from ('127.0.1.1', 61629)
Got connection from ('127.0.1.1', 61630)
Got connection from ('127.0.1.1', 61631)
Got connection from ('127.0.1.1', 61632)
Got connection from ('127.0.1.1', 61633)
Got connection from ('127.0.1.1', 61634)
Got connection from ('127.0.1.1', 61635)

客户机打印:

Thank you for connecting
时间: 2024-10-05 05:07:11

Beginning Python From Novice to Professional (9) - Socket的相关文章

Beginning Python From Novice to Professional (4) - 演示样本格式字符串

$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'P

Beginning Python From Novice to Professional (4) - 字符串格式示例

$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'P

Beginning Python From Novice to Professional (2) - 命令行运行Python脚本

命令行运行Python脚本 Linux下先创建一个hello.py [python] view plaincopy $ gedit hello.py 输入: [python] view plaincopy #!/usr/bin/env python print 2+2 保存退出,运行: [python] view plaincopy $ python hello.py 4 我们也可以让它变得和普通程序一样执行 执行之前,让脚本文件具备可执行属性: [python] view plaincopy

Beginning Python From Novice to Professional (3) - 列表操作

列表操作 list函数: [python] view plaincopy >>> list('hello') ['h', 'e', 'l', 'l', 'o'] 改变列表: [python] view plaincopy >>> x=[1,1,1] >>> x[1]=2 >>> x [1, 2, 1] 删除元素: [python] view plaincopy >>> names = ['wu','li','zhao

Beginning Python From Novice to Professional (7) - 类

类 创建简单类: #!/usr/bin/env python __metaclass__ = type class Person: def setName(self,name): self.name = name def getName(self): return self.name def greet(self): print "Hello,world! I'm %s." % self.name foo = Person() bar = Person() foo.setName('L

Beginning Python From Novice to Professional (8) - 文件方法

文件方法 读写: #!/usr/bin/env python f = open('somefile.txt','w') f.write('Hello,') f.write('World!') f.close() f = open('somefile.txt','r') print f.read(5) Hello 使用基本文件方法: #!/usr/bin/env python f = open(r'somefile.txt') print f.read() f.close() f = open(r

Beginning Python From Novice to Professional (6) - 函数使用

函数使用 定义函数: #!/usr/bin/env python def hello(name): return 'Hello, ' + name + '' print hello('world') print hello('Gumby') Hello, world Hello, Gumby 斐波那契序列举例: #!/usr/bin/env python def fibs(num): result = [0,1] for i in range(num-2): result.append(resu

Beginning Python From Novice to Professional (5) - 条件与循环

条件与循环 条件执行: name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby' What is your name? Gumby Hello, Mr.Gumby name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby' else: print 'He

Beginning Python From Novice to Professional (1) - 数字和表达式

数字和表达式 加减乘除: >>> 2+2 4 >>> 100-50 50 >>> 3*5 15 >>> 1/2 0 >>> 1.0/2.0 0.5 求余.乘方: >>> 1%2 1 >>> 10%3 1 >>> 2**3 8 >>> -3**2 -9 >>> (-3)**2 9 十六进制.八进制: >>> 0x