首页 > 代码库 > python socket和HTTP入门

python socket和HTTP入门

python迭代器:

iterator=iter(obj)

iterator.next()


python函数:

def function(*args):

传元组列表参数。

def function(**args):

传字典列表参数。


Django时区设置:setting.py里面的time_zone=Asian/Shanghai

PostgreSQL时区设置:postgres.conf配置文件修改time_zone=Asian/Shanghai


python socket模块:

服务器端:

import socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.bind((‘192.168.1.1‘,80))

s.listen(1)


while True:

    cs,ca=s.accept()

    print cs.recv(1024)

    cs.sendall(‘reply‘)

    cs.close()


客户端:

import socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect((‘192.168.1.1‘,80))

s.send(‘hello‘)

print s.recv(1024)

s.close()


python HTTP库:

import httplib

http=httplib.HTTPConnection(‘itercast.com’,80)

http.request(‘GET‘,‘/ask‘)

print http.getresponse().read()

http.close()


import urllib2 (更简单的实现HTTP,FTP等协议)

opener=urllib2.build_opener()

f=opener.open(‘http://itercast.com/ask‘)

print f.read()

f.close()


Ping linuxcast.net


完。

本文出自 “蓝鳍豚” 博客,请务必保留此出处http://likunheng.blog.51cto.com/9527645/1575509

python socket和HTTP入门