ts2009 发表于 2015-10-26 12:20:26

python socket

简介:


  socket只是一个类似于句柄一样的对象抽象
  如果实现的是tcp/ip协议,则socket是对TCP/IP协议栈操作的抽象,而不是简单的映射关系
  如果一个程序创建了一个socket,并让其监听80端口,其实是向TCP/IP协议栈声明了其对80端口的占有。
  以后,所有目标是80端口的TCP数据包都会转发给该程序(这里的程序,因为使用的是Socket编程接口,所以首先由Socket层来处理)。
  1.accept函数,其实抽象的是TCP的连接建立过程。
  accept函数返回的新socket其实指代的是本次创建的连接,而一个连接是包括两部分信息的,一个是源IP和源端口,另一个是宿IP和宿端口。
  所以,accept可以产生多个不同的socket,而这些socket里包含的宿IP和宿端口是不变的,变化的只是源IP和源端口。
  这样的话,这些socket宿端口就可以都是80,而Socket层还是能根据源/宿对来准确地分辨出IP包和socket的归属关系,从而完成对TCP/IP协议的操作封装!
  而同时,放火墙的对IP包的处理规则也是清晰明了,不存在前面设想的种种复杂的情形。



2.close函数,关闭TCP的链接
  http://blog.itpub.net/14766028/viewspace-703114

例子:


  

#coding=utf-8
import socket
def newsocket(host,port,pic_content,text_content):
HOST=host
PORT=port
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((HOST,PORT))
while True:
#最大接受3个request
s.listen(3)
conn,addr=s.accept()
request=conn.recv(1024)
method=request.split(' ')
src=request.split(' ')
if method=='GET':
if src == '/test.jpg':
content=pic_content
else:
content=text_content
print "req: %s, connected by %s"%(request,conn)
conn.sendall(content)
if method=="POST":
form=request.split('\r\n')
idx=form.index('')
entry=form
value=entry[-1].split('=')[-1]
conn.sendall(text_content+'\n<p>'+value+'<p>')
conn.close()

if __name__==&quot;__main__&quot;:
HOST = ''
PORT = 8000
# Prepare HTTP response
text_content = '''
<head>
<title>WOW</title>
</head>
<html>
<p>Wow, Python Server</p>
<IMG src=&quot;/test.jpg&quot;/>
</html>
'''
# Read picture, put into HTTP format
f = open('/home/liup/Documents/test.jpg','rb')
pic_content = '''
HTTP/1.x 200 OK   
Content-Type: image/jpg   
'''
pic_content = pic_content + f.read()
f.close()
text_contentpost = '''
<head>
<title>hello ithomer</title>
</head>
<html>
<p>hello, Python Server</p>
<img src=&quot;/home/homer/1_sunboy_2050.jpg&quot;/>
<form name=&quot;input&quot; action=&quot;/&quot; method=&quot;post&quot;>
First name:<input type=&quot;text&quot; name=&quot;firstname&quot;><br>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
</html>
'''
newsocket(HOST,PORT,pic_content,text_contentpost)


例子2


  server
  import socket
def newserver(url,host):
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((url,host))
s.listen(5)
while True:
conn,addr=s.accept()
print &quot;got connection from &quot;,addr
conn.send(&quot;hello, %s&quot; % (addr,))
message=conn.recv(1024)
print &quot; message is &quot;, message
conn.close()

if __name__==&quot;__main__&quot;:
newserver(&quot;127.0.0.1&quot;,8000)

client
  import socket
def newclient(url,host):
address=(url,host)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(address)
data=s.recv(1024)
print &quot;received data &quot;, data
s.send(&quot;hello world&quot;)
s.close()

if __name__==&quot;__main__&quot;:
newclient('127.0.0.1',8000)




  除了底层的socket模块,python还封装了一些高级的通讯模块
  SocketServer:架设服务器
  SimpleHTTPServer: 使用静态文件来回应请求
  CGIHTTPServer:使用静态文件或者CGI来回应请求


  
  


  



版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: python socket