起步于308的小菜菜

no money no dream coder 一枚

Python网络编程1

网络编程之socket

code

(server1.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
'''#/*=============================================================================
#
# Author: mengdaikun - mengdaikun@gmail.com
#
# QQ : 646978600
#
# Last modified: 2013-04-09 14:24
#
# Filename: server1.py
#
# Description: 
#
#=============================================================================*/'''
#server for python 

import sys,os,socket

host = ''
port = 5566

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 "Server is running on port %d;press Ctrl-C to terminate." % port

while 1:
    clientsock,clientaddr = s.accept()
    clientfile = clientsock.makefile('rw',0)
    clientfile.write("Welcome, " + str(clientaddr) + "\n")
    clientfile.write("Please enter a string:")
    line = clientfile.readline().strip()
    clientfile.write("You enter %d characters.\n" % len(line))
    clientfile.close()
    clientsock.close()

Comments