python-sockets

Difficulty using Python's socket.gethostbyaddr()

给你一囗甜甜゛ 提交于 2019-11-30 18:59:00
I am trying to reverse dns a list of IPs using socket.gethostbyaddr() in python, which returns 'Unknown Host' for some values, but using dig for the same ip returns the Hostname. Also, dig seems to be significantly faster than using python module, is there any specific reasons for that? import socket # This returns 'Unknown Host' name, alias, addresslist = socket.gethostbyaddr('114.143.51.197') I'm sorry, but you are mistaken. 114.143.51.197 does not have a PTR record... therefore socket.gethostbyaddr() should throw an error... you certainly need a try / except clause that traps for socket

Python Requests, how to bind to different source ip for each request? [duplicate]

假装没事ソ 提交于 2019-11-30 09:58:02
This question already has an answer here: Requests, bind to an ip 1 answer I'm trying to learn some python, and i'm having issues with the logic in what I want to test. Currently my code is written in a way that binding to source_address doesn't change when the process starts import socket import requests real_create_conn = socket.create_connection def set_src_addr(*args): address, timeout = args[0], args[1] source_address = ('201.X.X.1', 0) return real_create_conn(address, timeout, source_address) socket.create_connection = set_src_addr r = requests.get('http://www.mywebpage.com/main') print

python socket GET

强颜欢笑 提交于 2019-11-30 03:43:42
问题 From the other posts on stack overflow this should be working import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("www.cnn.com" , 80)) s.sendall("GET / HTTP/1.1\r\n") print s.recv(4096) s.close but for some reason it just hangs (at recv ) and never prints. I know that a request to www.cnn.com will chunk it's data but I should at least read something from recv , right? p.s. I know this isn't the best way to do it and that there are library like httplib and urllib2

Difficulty using Python's socket.gethostbyaddr()

放肆的年华 提交于 2019-11-30 03:04:14
问题 I am trying to reverse dns a list of IPs using socket.gethostbyaddr() in python, which returns 'Unknown Host' for some values, but using dig for the same ip returns the Hostname. Also, dig seems to be significantly faster than using python module, is there any specific reasons for that? import socket # This returns 'Unknown Host' name, alias, addresslist = socket.gethostbyaddr('114.143.51.197') 回答1: I'm sorry, but you are mistaken. 114.143.51.197 does not have a PTR record... therefore socket

Python requests speed up using keep-alive

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 22:05:55
In the HTTP protocol you can send many requests in one socket using keep-alive and then receive the response from server at once, so that will significantly speed up whole process. Is there any way to do this in python requests lib? Or are there any other ways to speed this up that well using requests lib? Yes, there is. Use requests.Session and it will do keep-alive by default . I guess I should include a quick example: import logging import requests logging.basicConfig(level=logging.DEBUG) s = requests.Session() s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') s.get('http:/

Python requests speed up using keep-alive

淺唱寂寞╮ 提交于 2019-11-28 16:59:27
问题 In the HTTP protocol you can send many requests in one socket using keep-alive and then receive the response from server at once, so that will significantly speed up whole process. Is there any way to do this in python requests lib? Or are there any other ways to speed this up that well using requests lib? 回答1: Yes, there is. Use requests.Session and it will do keep-alive by default. I guess I should include a quick example: import logging import requests logging.basicConfig(level=logging

Timeout for python coroutines

自闭症网瘾萝莉.ら 提交于 2019-11-28 00:31:50
How can I make a coroutine stop with timeout? I don't understand why asyncio.wait_for() doesn't work for me. I have such piece of code (planning to make my implementation of telnet client): def expect(self, pattern, timeout=20): if type(pattern) == str: pattern = pattern.encode('ascii', 'ignore') return self.loop.run_until_complete(asyncio.wait_for(self.asyncxpect(pattern), timeout)) async def asyncxpect(self, pattern): #receives data in a cumulative way until match is found regexp = re.compile(b'(?P<payload>[\s\S]*)(?P<pattern>%s)' %pattern) self.buffer = b'' while True: # add timeout # add

Timeout for python coroutines

女生的网名这么多〃 提交于 2019-11-26 21:43:30
问题 How can I make a coroutine stop with timeout? I don't understand why asyncio.wait_for() doesn't work for me. I have such piece of code (planning to make my implementation of telnet client): def expect(self, pattern, timeout=20): if type(pattern) == str: pattern = pattern.encode('ascii', 'ignore') return self.loop.run_until_complete(asyncio.wait_for(self.asyncxpect(pattern), timeout)) async def asyncxpect(self, pattern): #receives data in a cumulative way until match is found regexp = re