def send_mail(link, cookie, params=None, files=None): form = MultiPartForm() if files: n = 1 for f in files: form.add_file('file%s' % n, os.path.split(f)[1], fileHandle=open(f, 'rb')) n += 1 if params: for name, value in params: form.add_field(name, value) body = str(form) link = link.split('//')[-1] try: host, url = link.split('/', 1) header = 'POST /%s HTTP/1.0\r\nHost: %s\r\nUser-agent: Python; ' \ 'Android\r\nCookie: %s\r\nContent-type: %s\r\n' \ 'Content-length: %s\r\n\r\n' % ( url, host, cookie, form.get_content_type(), len(body)) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) client.connect((socket.gethostbyname(host), 80)) client.send(header + body) string = client.recv(1024).split('\r\n') for t in range(len(string)): line = string[t] if not line or line.isspace(): break header = '\r\n'.join(string[:t + 1]) up = header.split('\r\n', 1)[0] if up.find('200') == -1: return 0 L = [] string = '\r\n'.join(string[t + 1:]) L.append(string) while 1: t = client.recv(1024) if not t: break L.append(t) return ''.join(L) except Exception: return None
def test_1(self): # get ip addr import socket print socket.gethostbyname('www.xuanran001.com') # curl example: # curl -F key=test -F name="方案名称" -F descope="客厅" -F style="欧式" -F spereq="暂无" -F [email protected] www.xuanran001.com/d/api/putnewtask.html # Create the form with simple fields form = MultiPartForm() form.add_field('key', KEY) #form.add_field('umail', '_ceshi87_40wware.org') # Add a fake file #form.add_file('cankaotu[]', 'cankaotu1.jpg', # fileHandle=StringIO('spolo')) #form.add_file('cankaotu[]', 'cankaotu2.jpg', # fileHandle=StringIO('spolo')) #form.add_file('cankaotu[]', 'cankaotu3.jpg', # fileHandle=StringIO('spolo')) form.add_file('huxingtu', 'huxingtu.jpg', fileHandle=StringIO('spolo')) # Build the request request = urllib2.Request(URL) #request.add_header('User-agent', 'PyMOTW (http://www.doughellmann.com/PyMOTW/)') body = str(form) request.add_header('Content-type', form.get_content_type()) request.add_header('Content-length', len(body)) request.add_data(body) print print 'OUTGOING DATA:' print request.get_data() print print 'SERVER RESPONSE:' print urllib2.urlopen(request).read()
def deployByRepositoryId(self, stagedRepositoryId, artifact, userAgent=None): ''' Handles the upload of file. ''' url = '{}/deployByRepositoryId/{}/{}'.format(self._restApiUrl, stagedRepositoryId, artifact['relpath'].replace('\\', '/')) log.info('DEPLOY local file {} in staging repository {} to {}'.format(artifact['filename'], stagedRepositoryId, url)) form = MultiPartForm() f = open(artifact['path'], 'rb') form.add_file_handle('file', artifact['filename'], fileHandle=f) fileUploadHandle = form.file() f.close() headers = {} headers['Content-Type'] = form.get_content_type() if userAgent: headers['User-Agent'] = userAgent def uploadRequest(): return self._formbased_post_file(url, headers, fileUploadHandle, returnOnlyHttpCode=True) def uploadStatus(status): if status >= 200 and status < 400: log.info('\tthe deploy request was received by Nexus http {}'.format(status)) return 0 if status == 400: log.info('\tthe deploy request failed. Nexus returned http {}'.format(status)) return 2 if status > 400: log.info('\tthe deploy request failed. Nexus returned http {}'.format(status)) return 1 log.error('\tcannot check if upload is correctly executed on Nexus') return 2 def uploadRetryLog(tryNumber): log.info('\ttry {} to deploy on nexus'.format(tryNumber)) if self._requestManyTimes(uploadRequest, uploadStatus, nbTry=tuning.DEPLOY_FILE_REQ_ATTEMPTS, waitSecond=tuning.DEPLOY_FILE_DELAY_BETWEEN_REQ_ATTEMPTS, cbLogMessage=uploadRetryLog): def checkStatus(): return self.repository_content(stagedRepositoryId, artifact['relpath'].replace(artifact['filename'], '')) def isStatusValid(content_items): if content_items is None: log.error('\tcannot check if file is available on Nexus') return 1 for content_item in content_items: if 'text' in content_item and content_item['text'] == artifact['filename']: log.info('\tthe file is available on Nexus') return 0 log.warning('\tthe file is not fully uploaded yet... Nexus still working') return 2 if self._requestManyTimes(checkStatus, isStatusValid, cbLogMessage=lambda tryNumber: log.info('\ttry {} to check if file is uploaded on nexus'.format(tryNumber))): log.info('file {} deployed in staging repository {}'.format(artifact['filename'], stagedRepositoryId)) fileUploadHandle.close() return True # if response: # log.error(response) log.error('fail to deploy file {} in staging repository {}'.format(artifact['filename'], stagedRepositoryId)) fileUploadHandle.close() return False
def bundle_upload(self, artifactFolder, relativePath, userAgent=None): ''' Handles the upload of bundle. ''' log.info('UPLOAD BUNDLE in new staging repository') log.info('create bundle.zip') zf = zipfile.ZipFile(os.path.join(self._tmpFolder, 'bundle.zip'), mode='w') filesTocheck = [] relativePath = relativePath.replace('\\', '/') relativePath = relativePath[1:] if relativePath.startswith('/') else relativePath relativePath = relativePath[:-1] if relativePath.endswith('/') else relativePath try: for root, dirs, files in os.walk(artifactFolder): for name in files: filePath = os.path.join(root, name) filesTocheck.append(name) log.info('adding {}'.format(filePath)) zf.write(filePath, name) finally: log.info('closing bundle.zip') zf.close() form = MultiPartForm() form.add_file('file', 'bundle.zip', fileHandle=open(os.path.join(self._tmpFolder, 'bundle.zip'), 'rb')) body = str(form) url = '{}/{}'.format(self._restApiUrl, 'bundle_upload') headers = {} headers['Content-Type'] = form.get_content_type() headers['Content-Length'] = len(body) if userAgent: headers['User-Agent'] = userAgent response = self._request(url, headers, byteRequest=body) if response: oresponse = json.loads(response) if oresponse and 'repositoryUris' in oresponse and isinstance(oresponse['repositoryUris'], list): m = re.search(self._reJsonRespRepoId, oresponse['repositoryUris'][0]) if m: repoId = m.group('repoId') if repoId: def checkStatus(): content_items = self.repository_content(repoId, relativePath) if content_items is None: return None for filename in filesTocheck: for content_item in content_items: if 'text' in content_item and content_item['text'] == filename: break else: return 404 return 200 def isStatusValid(status): if status == 200: log.info('\tthe bundle is available on Nexus') return 0 if status is None: log.error('\tcannot check if bundle is available on Nexus') return 1 log.warning('\tthe bundle is not fully uploaded yet... Nexus still working resource respond {}'.format(status)) return 2 if self._requestManyTimes(checkStatus, isStatusValid, cbLogMessage=lambda tryNumber: log.info('\ttry {} to check if bundle is uploaded on nexus'.format(tryNumber))): log.info('bundle uploaded in staging repository {}'.format(repoId)) return m.group('repoId') log.error('fail to upload bundle in new staging repository') return None
def upload(self, stagedRepositoryId, artifact, description, userAgent=None): ''' Handles the upload of artifact. ''' log.info('UPLOAD artifact {}.{} in staging repository {}'.format(artifact['group'], artifact['artifact'], stagedRepositoryId)) form = MultiPartForm() form.add_field('r', stagedRepositoryId) form.add_field('g', artifact['group']) form.add_field('a', artifact['artifact']) form.add_field('v', artifact['version']) form.add_field('p', artifact['package']) form.add_field('c', artifact['classifier']) form.add_field('e', artifact['extension']) form.add_field('desc', description) form.add_file('file', artifact['filename'], fileHandle=open(artifact['path'], 'rb')) body = str(form) url = '{}/{}'.format(self._restApiUrl, 'upload') headers = {} headers['Content-Type'] = form.get_content_type() headers['Content-Length'] = len(body) if userAgent: headers['User-Agent'] = userAgent response = self._request(url, headers, byteRequest=body) if response: oresponse = json.loads(response) if 'repositoryId' in oresponse: repoId = oresponse['repositoryId'] if repoId: def checkStatus(): return self.repository_content(stagedRepositoryId, artifact['relpath'].replace(artifact['filename'], '')) def isStatusValid(content_items): if content_items is None: log.error('\tcannot check if file is available on Nexus') return 1 for content_item in content_items: if 'text' in content_item and content_item['text'] == artifact['filename']: log.info('\tthe file is available on Nexus') return 0 log.warning('\tthe file is not fully uploaded yet... Nexus still working') return 2 if self._requestManyTimes(checkStatus, isStatusValid, cbLogMessage=lambda tryNumber: log.info('\ttry {} to check if file is uploaded on nexus'.format(tryNumber))): log.info('artifact {}.{} uploaded in staging repository {}'.format(artifact['group'], artifact['artifact'], repoId)) return repoId log.error('fail to upload artifact {}.{} in staging repository {}'.format(artifact['group'], artifact['artifact'], stagedRepositoryId)) return None
def get_page(link, cookie, params=None, files=None): get_page.flag = 1 link = link.split("//", 1)[-1] try: host, url = link.split("/", 1) if not files: header = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-agent: " "Python; Android\r\nCookie: %s\r\n\r\n" % ( url, host, cookie, ) else: form = MultiPartForm() if files: n = 1 for f in files: if isinstance(f, str): form.add_file("file%s" % n, os.path.split(f)[1], fileHandle=open(f, "rb")) n += 1 elif isinstance(f, (tuple, list)): fl, nm = f form.add_file(nm, os.path.split(fl)[1], fileHandle=open(fl, "rb")) for name, value in params: form.add_field(name, value) body = str(form) header = ( "POST /%s HTTP/1.0\r\nHost: %s\r\nUser-agent: " "Python; Android\r\nCookie: %s\r\nContent-type: " "%s\r\nContent-length: %s\r\n\r\n" % (url, host, cookie, form.get_content_type(), len(body)) ) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) client.connect((socket.gethostbyname(host), 80)) assert get_page.flag if not files: client.send(header) else: client.send(header + body) assert get_page.flag string = client.recv(1024).split("\r\n") for t in range(len(string)): line = string[t] if not line or line.isspace(): break header = "\r\n".join(string[: t + 1]) up = header.split("\r\n", 1)[0] if up.find("200") == -1: return False L = [] string = "\r\n".join(string[t + 1 :]) L.append(string) while True: assert get_page.flag t = client.recv(1024) if not t: break L.append(t) page = "".join(L) return page except Exception as exc: return (-1, None)[get_page.flag]