免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4516 | 回复: 0
打印 上一主题 下一主题

[socket]Python HTTP通信,response数据是乱码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-04-04 00:25 |只看该作者 |倒序浏览
我还是Python网络编程新手,在网上找了一个PythonProxy的代理代码阅读,我打印response后的数据,但是在控制台中显示全部都是乱码,这是什么问题,是字符编码不对么。求助!
  1. #coding:UTF-8
  2. import socket, thread, select

  3. __version__ = '0.1.0 Draft 1'
  4. BUFLEN = 8192
  5. VERSION = 'Python Proxy/'+__version__
  6. HTTPVER = 'HTTP/1.1'

  7. class ConnectionHandler:
  8.     def __init__(self, connection, address, timeout):
  9.         self.client = connection
  10.         self.client_buffer = ''
  11.         self.timeout = timeout
  12.         self.method, self.path, self.protocol = self.get_base_header()
  13.         if self.method=='CONNECT':
  14.             self.method_CONNECT()
  15.         elif self.method in ('OPTIONS', 'GET', 'HEAD', 'POST', 'PUT',
  16.                              'DELETE', 'TRACE'):
  17.             self.method_others()
  18.         self.client.close()
  19.         self.target.close()

  20.     def get_base_header(self):
  21.         while 1:
  22.             self.client_buffer += self.client.recv(BUFLEN)
  23.             end = self.client_buffer.find('\n')
  24.             if end!=-1:
  25.                 break
  26.         print '%s'%self.client_buffer[:end]#debug
  27.         data = (self.client_buffer[:end+1]).split()
  28.         self.client_buffer = self.client_buffer[end+1:]
  29.         return data

  30.     def method_CONNECT(self):
  31.         self._connect_target(self.path)
  32.         self.client.send(HTTPVER+' 200 Connection established\n'+
  33.                          'Proxy-agent: %s\n\n'%VERSION)
  34.         self.client_buffer = ''
  35.         self._read_write()        

  36.     def method_others(self):
  37.         self.path = self.path[7:]
  38.         i = self.path.find('/')
  39.         host = self.path[:i]        
  40.         path = self.path[i:]
  41.         self._connect_target(host)
  42.         self.target.send('%s %s %s\n'%(self.method, path, self.protocol)+
  43.                          self.client_buffer)
  44.         self.client_buffer = ''
  45.         self._read_write()

  46.     def _connect_target(self, host):
  47.         i = host.find(':')
  48.         if i!=-1:
  49.             port = int(host[i+1:])
  50.             host = host[:i]
  51.         else:
  52.             port = 80
  53.         (soc_family, _, _, _, address) = socket.getaddrinfo(host, port)[0]
  54.         self.target = socket.socket(soc_family)
  55.         self.target.connect(address)

  56.     def _read_write(self):
  57.         time_out_max = self.timeout/3
  58.         socs = [self.client, self.target]
  59.         count = 0
  60.         while 1:
  61.             count += 1
  62.             (recv, _, error) = select.select(socs, [], socs, 3)
  63.             if error:
  64.                 break
  65.             if recv:
  66.                 for in_ in recv:
  67.                     data = in_.recv(BUFLEN)
  68.                     if in_ is self.client:
  69.                         out = self.target
  70.                     else:
  71.                         print '[RESPONSE]:'+data#在这里输出服务器返回的数据,但是都是乱码
  72.                         out = self.client
  73.                     if data:
  74.                         out.send(data)
  75.                         count = 0
  76.             if count == time_out_max:
  77.                 break

  78. def start_server(host='localhost', port=8080, IPv6=False, timeout=60,
  79.                   handler=ConnectionHandler):
  80.     if IPv6==True:
  81.         soc_type=socket.AF_INET6
  82.     else:
  83.         soc_type=socket.AF_INET
  84.     soc = socket.socket(soc_type)
  85.     soc.bind((host, port))
  86.     print "Serving on %s:%d."%(host, port)#debug
  87.     soc.listen(0)
  88.     while 1:
  89.         thread.start_new_thread(handler, soc.accept()+(timeout,))

  90. if __name__ == '__main__':
  91.     start_server()
复制代码
输出:
[RESPONSE]:E� �������u5�@t��ҋ�d3"�T
�ˮ��d�mt��ѹ��
SCE�8�(��� b�
jB/Z�˛e�ZI��J�;"�"F88{ci�sC����?STC���ܩX�����4��+.+�3�ׇ5�aiV�H?P�H�+�� /&V�w
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP